How to make gameObject (moving in sin wave) reach target position?

Hi,

Right now my gameObject is moving in sin wave through the following formula:

basePosition += transform.forward * speed * Time.deltaTime;
transform.position = basePosition + transform.up * amplitude * Mathf.Sin(omega * Time.time);

I do have a target transform reference as well so if we suppose the previous gameObject reached this target, the state of the gameObject itself will change as the follow:

if (transform.position == target.position)
state = State.End;

However, I don’t have any control over the gameObject position when it reach the target position since it moves in sin wave. It may pass the target position from above or down and sometimes it may reach it with slight chance.

I tried dividing amplitude/omega by the distance between gameObject and the target but it did not work. Trying even distribution to ensure this forward movement based on sin wave will end by the end to the specified target position exactly.

Any idea?

regards,
K

Just like you do it on a graph, 'cept you plug in time as X and output Y to height (or whatever axis you want to oscillate on.

transform.position.y = new Vector3(transform.position.x, Mathf.Sin(Time.time), transform.position.z);