Need help on how to animate game object

Hi! i am completely new in unity and i’m currently working on a simple platform type game. I’ve come to the part where I made a gameobject (a small platform) in which the character can stand on and I want it to move forwards and backwards. how do i do that. Thank you in advance.

1 Answer

1

You want to move the platform? You want to move back and forth the easiest way is to use a cos function I think

transform.position.x += A*Mathf.Cos(omega*Time.time);

A is the amplitude/travel distance, omega is the “frequency” or speed

Then it goes back and forth with a little slow down effect at the edge.

Try and see if that works.

Thanks it works!

Make sure to accept @fafase's answer by clicking the tick.

its working but how do I set the distance, for example from point a to point b

That should just be setting variable A?

If you want to "master" the function, you should check the principal of cos/sin function. You may know or not but this is a physical function that applies to many fields (Digital Signal Processing, Simple Harmonic Motion among many others), so it cannot hurt to know what you deal with. Simply put A is the distance, cos/sin just change if you start from 0 or 1 but they act the same way, omega is the frequency or how many times from peak to peak in 1 sec. Time is just there to make the whole thing go.