如何使用我的脚本准确切换通道?

How do I accurately switch lanes with my script?

我正在开发一款 3 通道 3d 无尽跑酷游戏,我遇到了这个问题。

在这个堆栈溢出的帮助下,我设法在 3 个车道上切换我的角色,几秒钟后我的角色慢慢地离开车道,而不是 运行 每个车道上的一条直线车道。切换角色后离开车道。这真的很烦人。我如何解决它?

我注意到角色的x轴,点值一点一点的增加。例如如果是正确的车道应该是1.0000,但切换后逐渐增加1.0045、1.0345、1.09585等等,反之亦然。有时它也会打破 3 车道移动,角色试图一直向右或向左移动而不停止,所以我不得不停止播放模式。

不胜感激。

这是我的脚本。

//Variables for Lane switching 
    private bool isChangingLane = false;
    private Vector3 locationAfterChanginLane = Vector3.zero;
    private Vector3 sideWayMovementDistance = Vector3.right * 2f; // This might be the case that triggers abnormal movements
    private float sideWaySpeed = 6f;

    public enum Lane
    {
        Left,
        Right,
        Center
    }

    public enum MoveDirection
    {
        Left,
        Right,
        None
    }

    Lane currentLane = Lane.Center;

     void Update()
    {
        currentBaseState = anim.GetCurrentAnimatorStateInfo(0);

        if (controller.isGrounded)
        {
            verticalVelocity = -0.5f;
            if (currentBaseState.fullPathHash == locoState)
            {
                if (Input.GetButtonDown("Jump"))
                {
                    verticalVelocity = 18f;

                    anim.SetBool("Jump", true);
                }
                else if (Input.GetKeyDown(KeyCode.S))
                {
                    anim.SetBool("Slide", true);
                }

            }

            MoveLeftRight(); // This is the method to move right and left.

            if (isChangingLane)
            {
                if (Math.Abs(transform.position.x - locationAfterChanginLane.x) < 0.1f)
                {
                    isChangingLane = false;
                    moveVector.x = 0;
                }
            }



        }
    }

    private void MoveLeftRight()
    {

        MoveDirection requestedMoveDirection = MoveDirection.None;
        if (Input.GetKeyDown(KeyCode.A) && !isChangingLane)
        {
            requestedMoveDirection = MoveDirection.Left;
            isChangingLane = true;
        }
        else if (Input.GetKeyDown(KeyCode.D) && !isChangingLane)
        {
            requestedMoveDirection = MoveDirection.Right;
            isChangingLane = true;
        }

        switch (requestedMoveDirection)
        {
            case MoveDirection.Right:
                if (currentLane == Lane.Right)
                {
                    Debug.Log("Right Lane");
                    break; //Do nothing when in right lane.

                }
                else if (currentLane == Lane.Center)
                {
                    locationAfterChanginLane = transform.position + sideWayMovementDistance;
                    moveVector.x = +sideWaySpeed;

                    currentLane = Lane.Right;
                    Debug.Log("Center --> Right");
                }
                else if (currentLane == Lane.Left)
                {
                    locationAfterChanginLane = transform.position + sideWayMovementDistance;
                    moveVector.x = +sideWaySpeed;

                    currentLane = Lane.Center;
                    Debug.Log("Left --> Center");
                }
                break;
            case MoveDirection.Left:
                if (currentLane == Lane.Left)
                {
                    Debug.Log("Left Lane");
                    break; //Do nothing when in left lane.
                }
                else if (currentLane == Lane.Center)
                {
                    locationAfterChanginLane = transform.position - sideWayMovementDistance;
                    moveVector.x = -sideWaySpeed;

                    currentLane = Lane.Left;

                    Debug.Log("Center --> Left");
                }
                else if (currentLane == Lane.Right)
                {
                    locationAfterChanginLane = transform.position - sideWayMovementDistance;
                    moveVector.x = -sideWaySpeed;

                    currentLane = Lane.Center;

                    Debug.Log("Right --> Center");
                }
                break;
        }
    }

很简单,在检查角色当前位置是否接近最终位置的代码部分,也设置最终位置。

if (Math.Abs(transform.position.x - locationAfterChanginLane.x) < 0.1f)
{
    isChangingLane = false;
    moveVector.x = 0;
    transform.position = locationAfterChanginLane; //Add this line
}

发生这种情况的原因本质上是 Update 函数的工作原理。 Monobehaviour 的 Update 不会在固定的时间步长上调用(我们称之为 Time.deltaTime)。因此,在大多数情况下,您角色的位置将over/undershoot最终值。

所以我终于找到了问题的答案,就这样吧。最简单的方法是使用 Clamp 来解决问题。这是我实现的。

Update函数中的Clamp方法。

public static float Clamp(float val, float min, float max)
    {
        return (val < min) ? min : (val > max) ? max : val;
    }

在我的 MoveLeftRight() 方法中,

switch (requestedMoveDirection)
        {
            case MoveDirection.Right:
                if (currentLane == Lane.Right)
                {
                    break; //Do nothing when in right lane.                    
                }
                else if (currentLane == Lane.Center)
                {
                    locationAfterChangingLane = transform.position + sideWayMovementDistance;
                    moveVector.x = +sideWaySpeed;

                    locationAfterChangingLane.x = Clamp(locationAfterChangingLane.x, RightLaneMin, RightLaneMax);
                    currentLane = Lane.Right;
                }
                else if (currentLane == Lane.Left)
                {
                    locationAfterChangingLane = transform.position + sideWayMovementDistance;
                    moveVector.x = +sideWaySpeed;

                    locationAfterChangingLane.x = Clamp(locationAfterChangingLane.x, centerLaneMin, centerLaneMax);
                    currentLane = Lane.Center;
                }
                break;

            case MoveDirection.Left:
                if (currentLane == Lane.Left)
                {
                    break; //Do nothing when in left lane.
                }
                else if (currentLane == Lane.Center)
                {
                    locationAfterChangingLane = transform.position - sideWayMovementDistance;
                    moveVector.x = -sideWaySpeed;

                    locationAfterChangingLane.x = Clamp(locationAfterChangingLane.x, leftLaneMin, leftLaneMax);
                    currentLane = Lane.Left;
                }
                else if (currentLane == Lane.Right)
                {
                    locationAfterChangingLane = transform.position - sideWayMovementDistance;
                    moveVector.x = -sideWaySpeed;

                    locationAfterChangingLane.x = Clamp(locationAfterChangingLane.x, centerLaneMin, centerLaneMax);
                    currentLane = Lane.Center;
                }
                break;
        }

给你。希望能帮助到你。感谢大家的帮助。编码愉快。