Moving an object in a spiral motion towards another

Hi community,

I’m trying to have one object move from a starting position to a target in a spiral motion, ideally stopping at the exact location of the target object (rather than ‘towards’ the target in a spiral). I’ve been scratching my head trying to figure out the maths of it - any pointers?

Basically, what you need are polar coordinates, where the position of the object is defined by an angle and the radius (in 2D) or two angles + radius (in 3D). You can then animate the angle(s) to make the object rotate around the target point and make the radius continuously shorter.

Something like this for a flat spiral in the horizontal plane (untested):

float angle, radius = 10;
float angleSpeed = 2;
float radialSpeed = 0.5f;

void Update() {
    angle += Time.deltaTime * angleSpeed;
    radius -= Time.deltaTime * radialSpeed;

    float x = radius * Mathf.Cos(Mathf.Deg2Rad*angle);
    float z = radius * Mathf.Sin(Mathf.Deg2Rad*angle);
    float y = transform.position.y;

    transform.position = new Vector3(x, y, z);
}
1 Like

Thanks Piflik, that’s useful.

I’m still a little confused as to how to keep the object moving toward the target and narrowing it’s radius as it gets closer (so that it actually ends up at the target position) though?

I know it’s been a long time, but did you manage to get any solution… If yes can you please share it.

The posted code is the solution

angle += Time.deltaTime * angleSpeed;
radius -= Time.deltaTime * radialSpeed;

This bit increases the angle over time, making it circle the target, and also reduces the circle radius over time, making it spiral inward until the radius is 0 and it’s ontop of the target.

Should probably clamp the radius at zero though with radius = Mathf.Max(0, radius);