1

I'm using Vector3.Lerp() to instantiate some prefabs at a set distance from each other. This works, but I've just started playing with Vector3.Slerp.

This creates a nice arc, however I'd like the arc to happen on either the X or Z axis, not the Y. So rather than arcing up into the air, the prefabs remain in contact with the ground at all times.

I've been reading up on euler local transforms however I'm not entirely sure if that's the right thing.

If anyone could offer some advice, or input, I'd greatly appreciate it.

Programmer
  • 121,791
  • 22
  • 236
  • 328
Tony
  • 3,587
  • 8
  • 44
  • 77

1 Answers1

3

I did a little tinkering (and can't claim to fully understand the reasoning as of now, or whether this is the only solution) but it seems as long as the y-component of the start and end input Slerp Vectors are zero, the object will only travel in the xz plane. Below is an example:

Vector3 startpos;
Vector3 endpos;
Vector3 yoffset;
float t;

void Start() 
{
    startpos = new Vector3(-5, 2, 0);
    endpos = new Vector3(3, 2, 2);
    yoffset = new Vector3(0, 2, 0);
}


void Update()
{
    t = (t+Time.deltaTime);
    transform.position = Vector3.Slerp(startpos - yoffset, endpos - yoffset, t/5f);
    transform.position += yoffset;
}
ryeMoss
  • 4,303
  • 1
  • 15
  • 34
  • You know what, I'm not entirely sure how that works either, but it does. Thanks! I was hoping that the Slerp just had an overload somewhere to control it, but this seems to do the job. I'll have a play with it some more. – Tony Aug 02 '17 at 07:14
  • 1
    I think this is because slerp isn't treating the start and end vector3 as points and interpolating between them - it's treating them as vectors and interpolating the angle and magnitude of them. If you have your vectors with zero y component then any angle interpolation between them will always be on the xz plane. If not, no guarantee, even if the y components of start and end are the same. – Weyland Yutani Aug 02 '17 at 10:55