solo365 solo365 - 1 year ago 105
C# Question

Move object with different speed

I have an object which is going up all the time. I want my object to go up at certain speed at first then with another speed after that (say, after 5 seconds). I am simply using

transform.Translate (Vector3.up * speed, Space.World);

but it's only going up at same speed all the time which I don't want.

Answer Source

You could simply change the speed after 5 seconds.

Your class would look like this:

public IEnumerator Start() {
    yield return new WaitForSeconds(5);
    speed *= 2;

public void Update() {
    transform.Translate(Vector3.up * speed * Time.deltaTime, Space.World);

It's important that you add * Time.deltaTime to your translation calculation because otherwise your actual speed will be different from one machine to another.

If you add or subtract to a value every frame chances are you should multiply with Time.deltaTime. When you multiply with Time.deltaTime you essentially express: I want to move this object 10 meters per second instead of 10 meters per frame.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download