Vector3.RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta)
用于旋转方向向量。
参数:
current: 当前方向向量
target: 目标方向向量
maxRadiansDelta: 每帧旋转角度=旋转速度*时间(deltaTime)
maxMagnitudeDelta: 希望返回的方向向量长度,最大有效值为目标方向向量(target)的长度,传0将返回单位向量长度
示例
using UnityEngine;
/// <summary>
/// 旋转方向向量
/// </summary>
public class APIRotateTowards : MonoBehaviour
{
[SerializeField]
private Transform target;
[SerializeField]
private float speed = 1f;
void Update()
{
Vector3 targetDir = target.position - transform.position;
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 5f);
Debug.DrawRay(transform.position, newDir, Color.red, 1f);
transform.rotation = Quaternion.LookRotation(newDir);
}
}