Unity lerp没有lerp,它只是立即移动

时间:2014-11-19 15:22:39

标签: c# unity3d

我不能为我的生活想出这个。这个确切的代码适用于我的Camera.Main对象。当我通过外部开关启动时,我的门需要Lerp向上,但出于某种原因,无论我放在那里它的价值,它都会立即跳起来,它不会像我希望的那样慢慢向上移动。这段代码放在我的void Update()块中,有什么想法吗?我做错了什么,我误解了Lerp的使用?

private Transform objTransform = null;
public bool isDoorOpen = false;
private Vector3 posDoorStart;
public Vector3 posDoorEnd;
public float DoorSmoothing = 2.0f;

// Use this for initialization
void Start () {
    objTransform = transform;
    posDoorStart = transform.position;
}

void Update() {
    if (isDoorOpen)
    {
        float smoothing = DoorSmoothing;
        // Also tried this...
        // float smoothing = DoorSmoothing * Time.deltaTime;
        transform.position = Vector3.Lerp(posDoorStart, posDoorEnd, smoothing);
    }
}

转换起始值: enter image description here

Gate脚本值: enter image description here

此外,有时如果我将lerp设置得太低,如本例所示,它只会部分移动到我想要的位置。它必须在1.5之前到达{{1}}才会一直向上,否则我会得到类似的东西......

enter image description here

1 个答案:

答案 0 :(得分:1)

让门完美地工作!非常流畅,isDoorOpen变量不是私有的。刚刚生成了一个公开的方法来开启门。试图将Time.time设置放在Start方法中,发现第一个动作愚蠢跳跃,在激活时设置并修复了所有问题。门是固定的。

private Transform objTransform = null;
private float startTime;
private float DistanctToTravel = 0.0f;
private bool isDoorOpen = false;
private Vector3 posDoorStart;

public Vector3 posDoorEnd;
public float DoorSmoothing = 1.0f;

void Start () {
    objTransform = transform;
    posDoorStart = transform.position;
}

void Update() {
    if (objTransform != null)
    {
        if (isDoorOpen)
        {
            float distCovered = (Time.time - startTime) * DoorSmoothing;
            float smoothing = distCovered / DistanctToTravel;
            transform.position = Vector3.Lerp(posDoorStart, posDoorEnd, smoothing);
        }
    }
}

public void OpenDoor(){
    startTime = Time.time;
    isDoorOpen = true;
    DistanctToTravel = Vector3.Distance (posDoorEnd, posDoorStart);
}