current | 正在管理的向量。 |
target | 向量。 |
maxRadiansDelta | 此旋转允许的最大弧度角。 |
maxMagnitudeDelta | 此旋转允许的最大向量幅度变化。 |
Vector3 RotateTowards 生成的位置。
将向量 current
旋转朝向 target
。
此函数类似于 MoveTowards,但向量被视为方向而不是位置。 current
向量将围绕 target
方向旋转 maxRadiansDelta
角,尽管它将准确地落在目标上而不是过度旋转。如果 current
和 target
的幅度不同,则结果的幅度将在旋转过程中进行线性插值。如果对 maxRadiansDelta
使用负值,则向量将远离 target/
旋转,直到它指向完全相反的方向,然后停止。
其他资源: Quaternion.RotateTowards.
using UnityEngine;
// To use this script, attach it to the GameObject that you would like to rotate towards another game object. // After attaching it, go to the inspector and drag the GameObject you would like to rotate towards into the target field. // Move the target around in the scene view to see the GameObject continuously rotate towards it. public class Example : MonoBehaviour { // The target marker. public Transform target;
// Angular speed in radians per sec. public float speed = 1.0f;
void Update() { // Determine which direction to rotate towards Vector3 targetDirection = target.position - transform.position;
// The step size is equal to speed times frame time. float singleStep = speed * Time.deltaTime;
// Rotate the forward vector towards the target direction by one step Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
// Draw a ray pointing at our target in Debug.DrawRay(transform.position, newDirection, Color.red);
// Calculate a rotation a step closer to the target and applies rotation to this object transform.rotation = Quaternion.LookRotation(newDirection); } }