摄像机跟随以及注视旋转和角度变化

前言

原理

  • 它是根据物体局部坐标系的描述来创建一个四元数
  • public static Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);
  • forward:向前向量, 即z轴的朝向。但是仅仅有z轴的朝向是不够的, 因为对象还可能绕着z轴旋转, 所以需要upwards来约束。
  • upwards:向上向量, 注意, 此向量不需要是精确的y轴的朝向(即不需要与forward垂直), 但是, 此向量应该处于z轴、y轴平面上。
  • 这样我们可以让一个物体持续注视某个物体
void Update() {
        Vector3 relativePos = target.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);
        transform.rotation = rotation;
    }

角度插值

  • Quaternion.Slerp按照圆弧进行插值,这个更适用于角度变换
  • transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed*Time.deltaTime)

猜你喜欢

转载自blog.csdn.net/qq_33574890/article/details/84316102