Vector3.RotateTowards
Declaration
public static Vector3 RotateTowards(Vector3 current, Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta);Параметры
| Параметр | Описание |
|---|---|
| текущий | Управляемый вектор. |
| Цель | Вектор. |
| 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); } }