版本:Unity 6 (6000.0)
语言英语
  • C#

Vector3.RotateTowards

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交的内容,但我们会阅读用户提出的每个建议更改,并在适用的情况下进行更新。

关闭

提交失败

由于某种原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static Vector3 RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta);

参数

current 正在管理的向量。
target 向量。
maxRadiansDelta 此旋转允许的最大弧度角。
maxMagnitudeDelta 此旋转允许的最大向量幅度变化。

返回值

Vector3 RotateTowards 生成的位置。

描述

将向量 current 旋转朝向 target

此函数类似于 MoveTowards,但向量被视为方向而不是位置。 current 向量将围绕 target 方向旋转 maxRadiansDelta 角,尽管它将准确地落在目标上而不是过度旋转。如果 currenttarget 的幅度不同,则结果的幅度将在旋转过程中进行线性插值。如果对 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); } }