# RotateTowards

> Rotates a vector current towards target.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.6/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## RotateTowards(Vector3, Vector3, float, float)

Rotates a vector `current` towards `target`.

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

### Parameters

**** (\[Vector3]\(/engine/6000.6/script-reference/unityengine/vector3)): The vector being managed.**** (\[Vector3]\(/engine/6000.6/script-reference/unityengine/vector3)): The vector.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The maximum angle in radians allowed for this rotation.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The maximum allowed change in vector magnitude for this rotation.

### Returns

| Type                                                              | Description                                |
| ----------------------------------------------------------------- | ------------------------------------------ |
| [Vector3](/engine/6000.6/script-reference/unityengine/vector3.md) | The location that RotateTowards generates. |

### Remarks

This function is similar to [Vector3.MoveTowards](/engine/6000.6/script-reference/unityengine/vector3/movetowards.md) 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.

Additional Resources: [Quaternion.RotateTowards](/engine/6000.6/script-reference/unityengine/quaternion/rotatetowards.md).

### Examples

```csharp
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);
    }
}
```

## RotateTowards(Vector3, Vector3, float, float)

Rotates a vector `current` towards `target`.

```csharp
public static Vector3 RotateTowards(in Vector3 current, in Vector3 target, float maxRadiansDelta, float maxMagnitudeDelta)
```

### Parameters

**** (\[Vector3]\(/engine/6000.6/script-reference/unityengine/vector3)): The vector being managed.**** (\[Vector3]\(/engine/6000.6/script-reference/unityengine/vector3)): The vector.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The maximum angle in radians allowed for this rotation.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The maximum allowed change in vector magnitude for this rotation.

### Returns

| Type                                                              | Description                                |
| ----------------------------------------------------------------- | ------------------------------------------ |
| [Vector3](/engine/6000.6/script-reference/unityengine/vector3.md) | The location that RotateTowards generates. |

### Remarks

This function is similar to [Vector3.MoveTowards](/engine/6000.6/script-reference/unityengine/vector3/movetowards.md) 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.

Additional Resources: [Quaternion.RotateTowards](/engine/6000.6/script-reference/unityengine/quaternion/rotatetowards.md).

### Examples

```csharp
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);
    }
}
```
