【问题标题】:How to make camera have same rotation as an object but with an offset (Unity)如何使相机具有与对象相同的旋转但具有偏移(Unity)
【发布时间】:2021-08-29 15:20:37
【问题描述】:

所以我有一辆汽车和一个相机,到目前为止,这里是相机脚本:

 public GameObject car;
    public Vector3 offset;

    void Start()
    {
        offset = transform.position - car.transform.position;
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        transform.position = transform.position = Vector3.Lerp(transform.position, car.transform.position + offset, 0.8f);
    }
}

这使相机随着汽车平滑移动,但现在我希望相机也具有与汽车相同的旋转,但具有起始偏移量。

【问题讨论】:

标签: c# unity3d rotation offset quaternions


【解决方案1】:

您可以以“相同”的方式存储原始增量旋转,例如

Quaternion offsetRotation;

private void Start ()
{
    offset = transform.position - car.transform.position;

    offsetRotation = transform.rotation * Quaternion.Inverse(car.transform.rotation);
}

后来

void FixedUpdate()
{
    transform.position = Vector3.Lerp(transform.position, car.transform.position + offset, 0.8f);

    transform.rotation = Quaternion.Slerp(transform.rotation, car.transform.rotation * offsetRotation, 0.8f);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多