0

im trying to make a script that allow my camera moving into 4 differents position ( front back left right ) using keyArrows and lerp.

the first movement works good, but when i hit another KeyArrow my camera move a little bit and get stucks between the first position and the end position.

There is the code :

void Update()
    {
        if (Input.GetKey(KeyCode.UpArrow)){
            Uparr = true;
        }
        if (Input.GetKey(KeyCode.DownArrow)){
            DownAarr= true;
        }
        if (Input.GetKey(KeyCode.RightArrow)){
            Rightarr = true;
        }
        if (Input.GetKey(KeyCode.LeftArrow)){
            Leftarr = true;
        }

        //boolean
        if(Uparr){
            cam.transform.LookAt(target);
            cam.transform.position = Vector3.Lerp(StartPos.position,endPosition1.position,lerpSpeed*Time.deltaTime);
            if (cam.transform.position == endPosition1.position){
                Uparr = false;
            }
        }
        if(DownAarr){
            cam.transform.LookAt(target);
            cam.transform.position = Vector3.Lerp(StartPos.position,endPosition2.position,lerpSpeed*Time.deltaTime);
            if (cam.transform.position == endPosition2.position){
                DownAarr = false;
            }
        }
        if(Rightarr){
            cam.transform.LookAt(target);
            cam.transform.position = Vector3.Lerp(StartPos.position,endPosition3.position,lerpSpeed*Time.deltaTime);
            if (cam.transform.position == endPosition3.position){
                Rightarr = false;
            }
        }
        if (Leftarr){
            cam.transform.LookAt(target);
            cam.transform.position = Vector3.Lerp(StartPos.position,endPosition4.position,lerpSpeed*Time.deltaTime);
            if (cam.transform.position == endPosition4.position){
                Leftarr = false;
            }
        }

do you know what it can be the problem ?

skehal
  • 13
  • 4

1 Answers1

0

You need to set the other direction variables to false when setting the new direction. Multiple views are active at once, and thus, they are fighting.

if (Input.GetKey(KeyCode.UpArrow)){
    Leftarr = false;
    Downarr = false;
    Rightarr = false;
    Uparr = true;
}

It may be easier to store one variable for direction but to each their own.

Ted Brownlow
  • 1,103
  • 9
  • 15