简体   繁体   English

如何使相机具有与 object 相同的旋转但有偏移(统一)

[英]How to make camera have same rotation as an object but with an offset (Unity)

So i have a car and a camera and so far here is the camera script:所以我有一辆汽车和一台相机,到目前为止,这里是相机脚本:

 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);
    }
}

This makes the camera smoothly move with the car, but now i want the camera to also have the same rotation as the car but with the starting offset.这使相机随着汽车顺利移动,但现在我希望相机也具有与汽车相同的旋转,但具有起始偏移。

You could just the "same" way store the original delta rotation like您可以以“相同”的方式存储原始增量旋转,例如

Quaternion offsetRotation;

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

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

And then later然后后来

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM