Vector3.Slerp problem


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?


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)