Home > Software design >  How to change the plane that Vector3.Slerp rotates on?
How to change the plane that Vector3.Slerp rotates on?

Time:12-06

As far as my understanding of Slerp is, in my case this is the way I should use the Vector3.Slerp function. I want to launch a sphere from the point start to the point target and rotate around the middle.

using UnityEngine;

public class Projectile : MonoBehaviour
{
    public Vector3 start;
    public Vector3 target;
    public Vector3 middle;

    public float flyTime;
    float launchTime;

    void Start()
    {
        launchTime = Time.time;
    }

    void Update()
    {
        float percentage = (Time.time - launchTime) / flyTime;
        transform.position = Vector3.Slerp(start - middle, target - middle, percentage)   middle;
    }
}

The thing is, the object will rotate along the up axis of the middle instead of along the right axis as I have seen it happens in the tutorials I've seen on YouTube.

This is the expected trajectory:

Trajectory around right axis of the middle

And this is what the trajectory that my code follows:

Trajectory around up axis of the middle

CodePudding user response:

Following the example below the SLerp documentation, you have to slightly offset the center of your rotation on Y axis in order to create a vertical arc.

Hope that helped ;)

  • Related