# Slerp

> Spherically interpolates between two three-dimensional vectors.

## Definition

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

## Slerp(Vector3, Vector3, float)

Spherically interpolates between two three-dimensional vectors.

```csharp
public static Vector3 Slerp(Vector3 a, Vector3 b, float t)
```

### Parameters

**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The first Vector3 direction to interpolate between.**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The second Vector3 direction to interpolate between.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The interpolation parameter with an expected value in the range \[0,1].

### Returns

| Type                                                              | Description                                               |
| ----------------------------------------------------------------- | --------------------------------------------------------- |
| [Vector3](/engine/6000.5/script-reference/unityengine/vector3.md) | The resulting spherically interpolated Vector3 direction. |

### Remarks

Interpolates between `a` and `b` by amount `t`. The difference between this and linear interpolation (aka, "lerp") is that the vectors are treated as directions rather than points in space. The direction of the returned vector is interpolated by the angle and its [Vector3.magnitude](/engine/6000.5/script-reference/unityengine/vector3/magnitude.md) is linearly interpolated between the magnitudes of `a` and `b`.

The parameter `t` is clamped to the range \[0, 1]. Setting `t==0` returns `a` whereas setting `t==1` returns `b`.

```csharp
// Animates the position in an arc between sunrise and sunset.

using UnityEngine;
using System.Collections;

public class Vector3SlerpExample : MonoBehaviour
{
    public Transform sunrise;
    public Transform sunset;

    // Time to move from sunrise to sunset position, in seconds.
    public float journeyTime = 1.0f;

    // The time at which the animation started.
    private float startTime;

    void Start()
    {
        // Note the time at the start of the animation.
        startTime = Time.time;
    }

    void Update()
    {
        // The center of the arc
        Vector3 center = (sunrise.position + sunset.position) * 0.5F;

        // move the center a bit downwards to make the arc vertical
        center -= new Vector3(0, 1, 0);

        // Interpolate over the arc relative to center
        Vector3 riseRelCenter = sunrise.position - center;
        Vector3 setRelCenter = sunset.position - center;

        // The fraction of the animation that has happened so far is
        // equal to the elapsed time divided by the desired time for
        // the total journey.
        float fracComplete = (Time.time - startTime) / journeyTime;

        transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
        transform.position += center;
    }
}
```

Additional Resources: [Vector3.Lerp](/engine/6000.5/script-reference/unityengine/vector3/lerp.md), [Vector3.SlerpUnclamped](/engine/6000.5/script-reference/unityengine/vector3/slerpunclamped.md).

## Slerp(Vector3, Vector3, float)

Spherically interpolates between two three-dimensional vectors.

```csharp
public static Vector3 Slerp(in Vector3 a, in Vector3 b, float t)
```

### Parameters

**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The first Vector3 direction to interpolate between.**** (\[Vector3]\(/engine/6000.5/script-reference/unityengine/vector3)): The second Vector3 direction to interpolate between.**** (\[float]\(https\://learn.microsoft.com/dotnet/api/system.single)): The interpolation parameter with an expected value in the range \[0,1].

### Returns

| Type                                                              | Description                                               |
| ----------------------------------------------------------------- | --------------------------------------------------------- |
| [Vector3](/engine/6000.5/script-reference/unityengine/vector3.md) | The resulting spherically interpolated Vector3 direction. |

### Remarks

Interpolates between `a` and `b` by amount `t`. The difference between this and linear interpolation (aka, "lerp") is that the vectors are treated as directions rather than points in space. The direction of the returned vector is interpolated by the angle and its [Vector3.magnitude](/engine/6000.5/script-reference/unityengine/vector3/magnitude.md) is linearly interpolated between the magnitudes of `a` and `b`.

The parameter `t` is clamped to the range \[0, 1]. Setting `t==0` returns `a` whereas setting `t==1` returns `b`.

```csharp
// Animates the position in an arc between sunrise and sunset.

using UnityEngine;
using System.Collections;

public class Vector3SlerpExample : MonoBehaviour
{
    public Transform sunrise;
    public Transform sunset;

    // Time to move from sunrise to sunset position, in seconds.
    public float journeyTime = 1.0f;

    // The time at which the animation started.
    private float startTime;

    void Start()
    {
        // Note the time at the start of the animation.
        startTime = Time.time;
    }

    void Update()
    {
        // The center of the arc
        Vector3 center = (sunrise.position + sunset.position) * 0.5F;

        // move the center a bit downwards to make the arc vertical
        center -= new Vector3(0, 1, 0);

        // Interpolate over the arc relative to center
        Vector3 riseRelCenter = sunrise.position - center;
        Vector3 setRelCenter = sunset.position - center;

        // The fraction of the animation that has happened so far is
        // equal to the elapsed time divided by the desired time for
        // the total journey.
        float fracComplete = (Time.time - startTime) / journeyTime;

        transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
        transform.position += center;
    }
}
```

Additional Resources: [Vector3.Lerp](/engine/6000.5/script-reference/unityengine/vector3/lerp.md), [Vector3.SlerpUnclamped](/engine/6000.5/script-reference/unityengine/vector3/slerpunclamped.md).
