我试图建立一个基于网格的地下城爬行器,我遇到了一个问题,其中旋转变换后。前进增加了x和z轴的值。尽管如此,玩家并没有向z轴移动,但碰撞检测认为它会停止移动。
public void RotateLeft()
{
if (AtRest) targetRotation -= transform.up * 90f;
}
public void RotateRight()
{
if (AtRest) targetRotation += transform.up * 90f;
}
public void MoveForward()
{
if (CanMove(Vector3.forward))
{
if (AtRest) targetGridPos += transform.forward;
}
}
public void MoveBackward()
{
if (CanMove(-Vector3.forward))
{
if (AtRest) targetGridPos -= transform.forward;
}
}
public void MoveLeft()
{
if (CanMove(-Vector3.right))
{
if (AtRest) targetGridPos -= transform.right;
}
}
public void MoveRight()
{
if (CanMove(Vector3.right))
{
if (AtRest) targetGridPos += transform.right;
}
}
bool CanMove(Vector3 direction)
{
if (direction.z != 0)
{
if (Physics.Raycast(zAxisOriginA, direction, rayLength)) return false;
if (Physics.Raycast(zAxisOriginB, direction, rayLength)) return false;
}
else if (direction.x != 0)
{
if (Physics.Raycast(xAxisOriginA, direction, rayLength)) return false;
if (Physics.Raycast(xAxisOriginB, direction, rayLength)) return false;
}
return true;
}
bool AtRest
{
get {
if((Vector3.Distance(transform.position, targetGridPos) < 0.05f) &&
(Vector3.Distance(transform.eulerAngles, targetRotation) < 0.05f))
return true;
else
{
return false;
}
}
}
1条答案
按热度按时间u4vypkhs1#
在我看来,你在Vector3方向上调用
CanMove
,这是绝对的,然后在变换方向上移动,这是相对于变换。让我们假设你的球员是面向右边,有一个墙直接在他们的前面。现在,如果球员试图向上移动(即他们自己的权利)碰撞检测功能将错误地阻止玩家这样做,尽管这是法律的的行为。这是因为Vector3.right面向墙壁,而transform.right将面向上。我建议在这种情况下始终使用变换方向。