Legacy Documentation: Version 2018.2 (Go to current version)
LanguageEnglish
  • C#

Vector3.RotateTowards

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

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

Parameters

currentThe vector being managed.
targetThe vector.
maxRadiansDeltaThe distance between the two vectors in radians.
maxMagnitudeDeltaThe length of the radian.

Returns

Vector3 The location that RotateTowards generates.

Description

Rotates a vector current towards target.

This function is similar to MoveTowards except that the vector is treated as a direction rather than a position. The current vector will be rotated round toward the target direction by an angle of maxRadiansDelta, although it will land exactly on the target rather than overshoot. If the magnitudes of current and target are different, then the magnitude of the result will be linearly interpolated during the rotation. If a negative value is used for maxRadiansDelta, the vector will rotate away from target/ until it is pointing in exactly the opposite direction, then stops.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { // The target marker. Transform target;

// Angular speed in radians per sec. float speed;

void Update() { Vector3 targetDir = target.position - transform.position;

// The step size is equal to speed times frame time. float step = speed * Time.deltaTime;

Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f); Debug.DrawRay(transform.position, newDir, Color.red);

// Move our position a step closer to the target. transform.rotation = Quaternion.LookRotation(newDir); } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答