简体   繁体   中英

Why my camera block between two position using lerp?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM