与玩家旋转无关的轴增加

huangapple go评论59阅读模式
英文:

Unrelated axis increasing after player rotates

问题

我正在尝试构建一个基于网格的地牢爬行游戏,但遇到了一个问题:在旋转后,transform.forward 增加了 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;
        }
    }
}

请注意,我没有翻译代码部分,只翻译了您提供的代码注释和方法名。如果需要其他帮助,请告诉我。

英文:

I am trying to build a grid based dungeon crawler and I'm running into a problem wherein after rotation transform.forward increases both the x and z axis values. Despite this the player does not move towards the z axis but the collision detection thinks it does, stopping movement.

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) &lt; 0.05f) &amp;&amp; 
            (Vector3.Distance(transform.eulerAngles, targetRotation) &lt; 0.05f))
            return true;
        else
        {
            return false;
        }
    }
}

答案1

得分: 1

从我看到的情况来看,你正在对一个绝对的 Vector3 方向调用 CanMove,然后在与 transform 相关的方向上移动。让我们假设你的玩家面向右边,正前方有一堵墙。现在,如果玩家尝试向上移动(即向右移动),碰撞检测函数将不正确地阻止玩家执行这个合法的操作。这是因为 Vector3.right 面向墙,而 tranform.right 面向上。我建议在这种情况下始终使用 transform 方向。

public void MoveForward()
{
    if (CanMove(transform.forward))
    {
        if (AtRest) targetGridPos += transform.forward;
    }
}
public void MoveBackward()
{
    if (CanMove(-transform.forward))
    {
        if (AtRest) targetGridPos -= transform.forward;
    }
}
public void MoveLeft()
{
    if (CanMove(-transform.right))
    {
        if (AtRest) targetGridPos -= transform.right;
    }
}
public void MoveRight()
{
    if (CanMove(transform.right))
    {
        if (AtRest) targetGridPos += transform.right;
    }
}
英文:

From what I see, you're calling CanMove on a Vector3 direction which is absolute, then moving in the transform direction which is relative to the transform. Let's assume your player is facing to the right and there is a wall directly ahead of them. Now if the player attempts to move up (i.e. right of themselves) the collision detection function would incorrectly prevent the player from doing so despite it being a legal action. This is because Vector3.right is facing the wall, while tranform.right would be facing up. I would recommend that you always use transform directions in this case.

public void MoveForward()
{
    if (CanMove(transform.forward))
    {
        if (AtRest) targetGridPos += transform.forward;
    }
}
public void MoveBackward()
{
    if (CanMove(-transfrom.forward))
    {
        if (AtRest) targetGridPos -= transform.forward;
    }
}
public void MoveLeft()
{
    if (CanMove(-transfrom.right))
    {
        if (AtRest) targetGridPos -= transform.right;
    }
}
public void MoveRight()
{
    if (CanMove(transform.right))
    {
        if (AtRest) targetGridPos += transform.right;
    }
}


huangapple
  • 本文由 发表于 2023年4月4日 03:50:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定