-1

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

这是Game master检查用户输入并将其传递给Player controller执行以下操作的脚本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;
        }
    }
}

播放器控制器:

public void MovePlayer (string dir)
{
    if (dir == "left")
    {
        if (currentPosition == Position.Left)
        {
            //Do Nothing!
            return;
        }

        if (currentPosition == Position.Middle)
        {
            transform.position = Vector3.Lerp(middlePos.position, leftPos.position, .5f);
            currentPosition = Position.Left;
        }

        if (currentPosition == Position.Right)
        {
            transform.position = Vector3.Lerp(rightPos.position, middlePos.position, .5f);
            currentPosition = Position.Middle;
        }
    }

    if (dir == "right")
    {
        if (currentPosition == Position.Right)
        {
            //Do Nothing!
            return;
        }

        if (currentPosition == Position.Middle)
        {
            transform.position = Vector3.Lerp(transform.position, rightPos.position, .5f);
            currentPosition = Position.Right;
        }

        if (currentPosition == Position.Left)
        {
            transform.position = Vector3.Lerp(transform.position, middlePos.position, .5f);
            currentPosition = Position.Middle;
        }
    }
}

截屏:

在此处输入图像描述

为什么会这样?

4

1 回答 1

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;
    }
}
于 2018-04-11T11:34:05.840 回答