// 摘要:
// Creates a rotation with the specified forward and upwards directions.
//
// 参数:
// forward:
// The direction to look in.
//
// upwards:
// The vector that defines in which direction up is.
例子:
using UnityEngine;
using System.Collections;
/*
*
*/
public class ExampleClass : MonoBehaviour {
public Transform player;
public Transform target;
void Update() {
Vector3 relativePos = target.position - player.position;
//relativePos.y = 0;//player物体朝向target物体时默认y轴会有倾斜,如果不想倾斜,可设置y轴坐标为0
player.rotation = Quaternion.LookRotation(relativePos);
}
}
**仅仅使用Quaternion中的LookRotation方法,可以使当前物体在一瞬间就朝向目标物体。假使要物体慢慢旋转朝向目标物体,就需要再加上插值运算:
Quaternion.Lerp或者Quaternion.Slerp
using UnityEngine;
using System.Collec