- Home /
detaliled explanation about given Vector3.Slerp example in unity documentation
// Animates the position in an arc between sunrise and sunset.
var sunrise : Transform;
var sunset : Transform;
function Update () {
// The center of the arc
var center = (sunrise.position + sunset.position) * 0.5;
// move the center a bit downwards to make the arc vertical
center -= Vector3(0,1,0);
// Interpolate over the arc relative to center
var riseRelCenter = sunrise.position - center;
var setRelCenter = sunset.position - center;
transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, Time.time);
transform.position += center;
}
this is the example given in the unity documentation, its little difficult to understand for a newbie like me, can anyone make it more understand with more simple example. thanks :)
Answer by Bunny83 · Jan 31, 2013 at 01:27 AM
Maybe this helps a bit (my MSPaint skills are a bit rusty :D)
edit I should have mentioned that this image is only correct if the start and end point is about the same distance from the origin. If they have different length the arc might look strange. I've made a webplayer to show this
This pic with robertbu's description of how the slerp moves like a pole from the origin helped me quite a bit. Thanks for the help.
@Novemb3r: I've added a webplayer where you can try out different situations since that "pole from the origin" is only true if both points are at the same distance from the origin. This is usually the case for normalized direction vectors and there it's most useful.
Answer by robertbu · Jan 29, 2013 at 07:58 AM
Here is a shorter example. Attach it to a game object, set the time.
private var endPos : Vector3 = new Vector3(2.0, 0.0, 0.0);
private var startPos : Vector3 = new Vector3(-2.0, 0.0, 0.0);
var translationTime : float = 3.0;
private var timer = 0.0;
function Start() {
transform.position = startPos;
}
function Update () {
if (timer < translationTime) {
transform.position = Vector3.Slerp(startPos, endPos, timer/translationTime);
timer += Time.deltaTime;
}
else if (timer >= translationTime) {
transform.position = endPos;
}
}
This code take the initial position of the game object and moves it to the endPos along an arc in the amount of time specified as translationTime. Slerp()'s final parameter is designed to be in the range of 0 to 1. It specifies what fraction the way between startPos and endPos to return. For a value of 0.5, the position will be half way between the two startPos and endPos.
Here is an even simpler example. It uses a GUI.VerticalSlider(). The slider's $$anonymous$$/max are set to 0.0 and 1.0.
private var endPos : Vector3 = new Vector3 (2.0, 0.0, 0.0);
private var startPos : Vector3 = new Vector3(-2.0f, 0.0, 0.0);
private var sliderValue : float = 0.0;
function Start() {
transform.position = startPos;
}
function Update () {
transform.position = Vector3.Slerp(startPos, endPos, sliderValue);
}
function OnGUI() {
sliderValue = GUI.VerticalSlider (Rect (25, 25, 50, 250), sliderValue, 0.0, 1.0);
}
thanks robertbu, i am very beginner to unity trying my best to understand your examples, can you plz do me another small favor i wrote this very simple script to understand slerp, its working but all i want to know how can i control the height of curve ? thanks.
var start : Transform; var end : Transform; function Update () { transform.position = Vector3.Slerp(start.position, end.position, Time.time); }
Imagine a rod going from the origin (0,0) out to the starting point you specify. Using the origin as a pivot/hinge, Slerp() will rotate over time until the end of the rod lands at the ending point. In rotating between these two points, Slerp takes the shortest distance. Take these following sets of points:
[(-2.0,0.0,-0.01), (2.0,0.0,-0.01)] - the shortest distance will be on the XZ plane. With a standard camera setup this will rotate across the X axis..
[(-2.0,0.01,0.0), (2.0,0.01, 0.0)] - The shortest path is directly across the top...the XY Plane.
[(-2.0,0.01,-0.01), (2.0,0.01, -0.01)] - The shortest path is at a 45 degree with respect to the X axis.
So to "control the height" is a combination of the distance the points are from the origin and where the shortest path is between the two points.
So looking into this explanation on slerps, I'm still confused as to how to control the height. If you are trying to simulate a ball toss...do you have to first position the ball at the mid point of both the start and end, and then perform the slerp? So that means you first need to calculate the mid point of two positions, spawn the object at the mid point, and then slerp the ball?
Without more context it difficult to give you an answer for your specific situation. And it may be that a solution other than Slerp would be better fit for your situation. I suggest you open a new UA question. As for Slerp(), it has nothing to do with game objects. It just deals with vectors. You may have to find the mid-point between two positions in order to generate the vectors you need for a specific Slerp(), but you don't have to spawn the object at the midpoint. Note Slerp() deals with vectors, so you will need to add position information back into your positioning logic to get the ball to arc between two positions.
Your answer
Follow this Question
Related Questions
doing force and spin on a gameobject when clicking on plane. 2 Answers
Smooth rotation problem 1 Answer
Rotate slowly to target position? 3 Answers
Rotate object towards mouse cursor when click 1 Answer
Issue With Rotation on Key Press 1 Answer