- Home /
Vector3.Slerp problem
Hello,
I have this bit of code, which should change the y co-ordinate (and nothing else) of the GameObject it's attached to.
using UnityEngine;
using System.Collections;
public class slerpTest : MonoBehaviour {
// Use this for initialization
void Start () {
transform.position = new Vector3(0,-20,0);
}
// Update is called once per frame
void Update () {
transform.position = Vector3.Slerp (transform.position, new Vector3(0,1,0), Time.deltaTime);
Debug.Log (transform.position);
}
}
Now when I run this, the z co-ordinate is changing, which it shouldn't. I've noticed it only happens when I'm slerping from a negative y value to a positive y value. If I change the slerp code to
transform.position = Vector3.Slerp(transform.position, new Vector3(0,0,0), Time.deltaTime);
then the z value doesn't change and it works as intended. I can't find any information anywhere on unity answers or google about problems regarding slerping from a negative value to a positive one.
Is it a bug? Is it normal behaviour? and is there a fix?
Thanks
Answer by aldonaletto · Aug 10, 2012 at 12:58 AM
I suspect you really want to use Lerp, not Slerp: Lerp returns a position on the line connecting the two points, while Slerp tries to return positions over a curve between the two points - and this curve may be very weird and unexpected depending on the distances of each point to the origin (0,0,0)
Thanks, when I use Lerp it works properly. I was somewhat surprised at it though as I regularly use Slerp in unity and have never come across this problem before.
Slerp is a very weird and unpredictable function: it returns points of a curve passing through the two end points, but nobody knows which mysterious algorithm evaluates this curve - small differences may change it completely. I tested the two endpoints you're using, and when the second point was (0,0,0), the "curve" was a straight line; when changed to (0,1,0), it became an spiral running in the opposite direction!