Vector3.Lerp仅执行第一次线性插值

时间:2018-04-08 03:44:35

标签: c# unity3d lerp

当用户在键盘上向左或向右输入时,我试图让玩家宇宙飞船在三点之间移动。我希望玩家在这些点之间平滑移动,但似乎Lerp函数只插入一次。

以下是Game master脚本,用于检查用户的输入并将其传递到执行Player controller的{​​{1}}:

游戏大师:

Lerp

玩家控制器:

void Update ()
{
    if (gameIsRunning)
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            //Go Left
            player.MovePlayer("left");
        }
        else if (Input.GetKeyDown(KeyCode.D))
        {
            //Go Right
            player.MovePlayer("right");
        }

        //Only run this if the game is running...
        if (player.Lives <= 0)
        {
            gameIsRunning = false;
        }
    }
}

截图:

enter image description here

为什么会这样?

1 个答案:

答案 0 :(得分:0)

必须使用更改参数调用

lerp。尝试类似的事情:

玩家控制器:

enum Direction
{
    Left,
    Right
}

Direction moveDir;
float progress;

public void MovePlayer (Direction dir)
{
    moveDir = dir;
    progress = 0.0f;
}

public void Update()
{
    switch(dir)
    {
        case Direction.Left:
        if (currentPosition == Position.Middle)
        {
            transform.position = Vector3.Lerp(middlePos.position, leftPos.position, progress);
            if(progress >= 1.0f)
                currentPosition = Position.Left;
            else
                progress += 0.1f; // change as necessary
        }

        if (currentPosition == Position.Right)
        {
            transform.position = Vector3.Lerp(rightPos.position, middlePos.position, progress);
            if(progress >= 1.0f)
                currentPosition = Position.Middle;
            else
                progress += 0.1f; // change as necessary
        }
        break;
        case Direction.Right:
        // ...
        break;
    }
}
相关问题