solo365
solo365

Reputation: 65

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.

Upvotes: 1

Views: 216

Answers (1)

sokkyoku
sokkyoku

Reputation: 2221

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.

https://docs.unity3d.com/ScriptReference/Time-deltaTime.html

Upvotes: 1

Related Questions