【问题标题】:Random rotation happening, I don't get it随机旋转,我不明白
【发布时间】:2021-10-02 08:58:07
【问题描述】:

我有一个场景如下: 我有播放器对象,它是一个 Z 锁定的立方体,我使用脚本移动它,使用各种 AddForce 调用。里面的重要方法是这个:

public void SetDirections(Vector3 down)
    {
    down.Normalize();
    this.down = down;
    forward = Vector3.Cross(down, new Vector3(0, 0, 1));
    }

同一脚本中与旋转相关的其他代码如下:

void Turning ()
    {
    // Irányitó gombok hatására a Taxi elforduljon 180fokot
    Vector3 lookDirection = forward;

    // balra forditás
    if (inputh > 0)
        {
        faceLeft = false;
        }

    // jobbra forditás
    if (inputh < 0)
        {
        faceLeft = true;
        }

    if (faceLeft)
        {
        lookDirection *= -1;
        }

    transform.LookAt(transform.position + lookDirection, new Vector3(0, 1, 0));
    }

我在场景中的另一个对象上有这个位:

public class RotateGravity : MonoBehaviour
{
public GameObject Taxi;
public float MinLimit, MaxLimit;
private TaxiController tControl;
private Rigidbody tRigidbody;
private Transform tTransform;

void Start()
    {
    tControl = Taxi.GetComponent<TaxiController>();
    tRigidbody = Taxi.GetComponent<Rigidbody>();
    tTransform = Taxi.GetComponent<Transform>();
    }


void OnTriggerStay()
    {
    Vector3 originalDown = tControl.Down;
    Vector3 newDown = tTransform.position - gameObject.transform.position;

    if (Vector3.SignedAngle(newDown, new Vector3(1, 0, 0), new Vector3(0, 0, 1)) > MinLimit &&
        Vector3.SignedAngle(newDown, new Vector3(1, 0, 0), new Vector3(0, 0, 1)) < MaxLimit)
        {
        newDown.Normalize();
        tControl.SetDirections(newDown);
        float angle = Vector3.SignedAngle(originalDown, newDown, new Vector3(0, 0, 1));
        tRigidbody.velocity = Quaternion.AngleAxis(angle, new Vector3(0, 0, 1)) * tRigidbody.velocity;
        }

    Debug.Log("Stay: " + newDown);
    }

void OnTriggerExit()
    {
    float angle = Vector3.Angle(tTransform.position - gameObject.transform.position, new Vector3(1, 0, 0));
    Vector3 newDown;
    float newAngle;
    
    if (Mathf.Abs(angle - MinLimit) < Mathf.Abs(angle - MaxLimit))
        {
        newDown = Quaternion.AngleAxis(MinLimit, new Vector3(0, 0, -1)) * new Vector3(1, 0, 0);
        newAngle = MinLimit;
        }
        else
        {
        newDown = Quaternion.AngleAxis(MaxLimit, new Vector3(0, 0, -1)) * new Vector3(1, 0, 0);
        newAngle = MaxLimit;
        }

    Debug.Log("Exit: " + newDown + " " + newAngle);

    tControl.SetDirections(newDown);
    tRigidbody.velocity = Quaternion.AngleAxis(newAngle, new Vector3(0, 0, 1)) * tRigidbody.velocity;
    }
}

基本上这个想法是当玩家沿着物体移动时,重力方向会发生变化,所以只要向右移动,它就会在 MinLimit 和 MaxLimit 角度之间围绕物体旋转。它确实如此。最后,我想将方向固定到这些限制,这就是奇怪的事情发生的地方。在我的示例中,MinLimit 为 0,MaxLimit 为 90。这意味着玩家从左侧水平进入,重力从 (0, -1, 0) 变为 (1, 0, 0)。除了,最后玩家对象围绕其前向矢量旋转 90 度。

谁能解释我哪里出错了?我讨厌 Unity 中的旋转 :(

在下面的视频中,红色球体只是玩家对象下方的一个指示器,以更好地显示其朝向。 https://www.youtube.com/watch?v=z4p9UQb7inA

【问题讨论】:

    标签: c# unity3d rotation


    【解决方案1】:

    好的,我没有在 Turning() 方法中正确设置向上向量,它指向正 Y 方向,所以当我的向前方向最终相同时,它不知道在哪里旋转对象轴。我很笨。我将向上向量设置为指向 -1 * 向下,然后砰,它起作用了。

    【讨论】:

    • 那么如果现在前锋指向下方会不会是同样的问题呢?
    • 不,因为变量 down 包含玩家当前的重力方向,这与您可能想到的 (0, -1, 0) 不同。所以最后向下包含一个向右的指向向量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多