我正在进行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;
}
}
答案 0 :(得分:2)
相当简单,在代码中检查角色当前位置是否接近最终位置的部分,也设置最终位置。
if (Math.Abs(transform.position.x - locationAfterChanginLane.x) < 0.1f)
{
isChangingLane = false;
moveVector.x = 0;
transform.position = locationAfterChanginLane; //Add this line
}
发生这种情况的原因主要是Update
函数的工作原理。 Monobehaviour的更新不是在固定时间步骤(我们称之为Time.deltaTime
)上调用的。因此,在大多数情况下,您角色的位置将超过/低于最终值。
答案 1 :(得分:0)
所以我终于找到了我对这个问题的答案,所以在这里。最简单的是使用Clamp来解决问题。这是我实施的内容。
Update函数中的Clamp方法。
public static float Clamp(float val, float min, float max)
{
return (val < min) ? min : (val > max) ? max : val;
}
在我的MoveLeftRight()
menthod中,
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;
}
你去吧。希望能帮助到你。谢谢大家的帮助。快乐的编码。