我如何准确地与我的脚本切换车道

本文关键字:脚本 我的 何准确 | 更新日期: 2023-09-27 18:02:18

我正在制作一款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)上调用的。因此,在大多数情况下,你的角色的位置将超过/低于最终值。

我终于找到了这个问题的答案。最简单的方法是使用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;
        }

好了。希望能有所帮助。谢谢大家的帮助。快乐的编码。