Version: 2017.4
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Vector3.Slerp

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 method Slerp(a: Vector3, b: Vector3, t: float): Vector3;
public static Vector3 Slerp(Vector3 a, Vector3 b, float t);

Description

Spherically interpolates between two vectors.

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 magnitude is interpolated between the magnitudes of from and to.

The parameter t is clamped to the range [0, 1].

#pragma strict
// Animates the position in an arc between sunrise and sunset.
public var sunrise: Transform;
public var sunset: Transform;
public var journeyTime: float = 1.0f;
private var startTime: float;
function Start() {
	// Note the time at the start of the animation.
	startTime = Time.time;
}
function Update() {
	// The center of the arc
	var center: Vector3 = (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
	var riseRelCenter: Vector3 = sunrise.position - center;
	var setRelCenter: Vector3 = sunset.position - center;
	// the total journey.
	var fracComplete: float = (Time.time - startTime) / journeyTime;
	transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, fracComplete);
	transform.position += center;
}
// Animates the position in an arc between sunrise and sunset.

using UnityEngine; using System.Collections;

public class ExampleClass : 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; } }

See Also: Lerp, SlerpUnclamped.