简体   繁体   English

为什么我的相机使用 lerp 挡在两个 position 之间?

[英]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.我正在尝试制作一个脚本,允许我的相机使用 keyArrows 和 lerp 进入 4 个不同的 position(前后左右)。

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.第一个动作很好,但是当我按下另一个 KeyArrow 时,我的相机移动了一点,卡在了第一个 position 和最后一个 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.设置新方向时,需要将其他方向变量设置为 false。 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.为方向存储一个变量可能更容易,但每个变量都有自己的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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