Explanation of slerp example in reference

Hey, the Vector3 slerp example in the Script reference contains the following. I’ve commented what I understand from it and reading wikipedia on slerp.

``````using UnityEngine;
using System.Collections;

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

void Update() {
// Is this first line a typo? When I tried this I divided the sum of
// both positions instead of doing what they do here and it seemed
// to work like it was supposed to. E.g. the object arced y-axis. In
// contrast, using the code here caused it to arc on the z-axis. Anyway
// this should be the midpoint between both positions.
Vector3 center = sunrise.position + sunset.position * 0.5F;

// Offset the midpoint so we now have the center of our arc. However, it's
// offset in the negative y direction, whereas you'd think it would be offset
// in the positive y direction.
center -= new Vector3(0, 1, 0);

// Sunrise position relative to the center, so this will be in the positive
// y direction.
Vector3 riseRelCenter = sunrise.position - center;

// Sunset position relative to the center, in +y direction.
Vector3 setRelCenter = sunset.position - center;

// Magic. Reference says 'interpolates from toward to by amount t'. The t
// argument isn't confusing, I get that they are just taking advantage of
// using the decimal values during the first second. But what are the first
// two parameters supposed to be exactly? Are they supposed to be start and
// end positions relative to our center? Again though, why is the center
// located below where the actual center of the arc is (at least from what
// I saw by running the example).
transform.position = Vector3.Slerp(riseRelCenter, setRelCenter, Time.time);

// No idea.
transform.position += center;
}
}
``````

I tried the above just having a cube arc from one position from another, with the only change being the center being originally calculated as (sunrise.position + sunset.position) * 0.5 rather than sunrise.position + (sunset.position * 0.5) as above. The cube in the +y direction from one point to another. If I left the example was it was without changing the first statement it arced in the positive z direction.

Just looking for an explanation of what’s going on. Particularly the reason for calculating center in that way, and adding center to the position at the end.

1- You’re right about the center calculation: it’s a typo in the C# example - the javascript version is ok.

2- The reason to move the center a little down is to force the movement to occur above the two points - slerp probably goes in the shortest direction, thus shifting the center a little below the line between the two points makes slerp “choose” the arc above them.

3- Apparently, slerp follows an arc centered at (0,0,0); since the calculated center may be anywhere, the smart Unity guys shifted the whole thing to (0,0,0) by subtracting the center coordinates from sunrise and sunset: they calculate slerp using these shifted from and to points, then add the center coordinates again to move the calculated point to where it should be - that’s the purpose of the mysterious last line.

4- The 3rd parameter is a number between 0 and 1, where 0 returns from position, 1 returns to position, and any value between these values return a proportional position in the arc.