How to Get Speed to Very Gradually Pick Up Over Time?

Hello all,

Very simple with what is probably a very simple solution, but I just cannot figure this one out.

My game is an over-the-shoulder endless runner style game. I have the obstacles coming down the screen with this VERY simple script:

using UnityEngine;
using System.Collections;

public class ObstacleMover : MonoBehaviour {

	private Rigidbody rb; // Reference to Rigidbody Component
	public float speed;

	void Start() 
	{
		rb = GetComponent<Rigidbody> ();
		// In Unity 5, you have to now have a reference to a component.
		rb.velocity = -transform.forward * speed;
		/* Transform is negative because in my game,  I have the
		   illusion of the objects coming down my mountain, towards the
		   player, where the camera is attached as a child.
		*/
	}
}

My question is, how do I make the ‘speed’ make the obstacle go gradually faster as the game progresses and the player goes longer into the game AND then have the speed top out (or just stop increasing and just stay constant) after a good while of the player surviving? I tried a few ideas with Mathf.Clamp, Time.deltaTime, and Vector3.MoveTowards, but they just did not give me the desired results. Thank you to all who attempt and help me solve this! :slight_smile:

try something like this:

private Rigidbody rb;      //Reference to Rigidbody Component
public float speed;        //Speed, updated through script
public float acceleration; //Every second, the speed will increase by this much

//Executes once, when object is spawned / scene loaded
void Start() {
    //Get reference to rigidbody, and set the speed
    rb = GetComponent<Rigidbody> ();
    rb.velocity = -transform.forward * speed;
}

//Executes every frame
void Update() {
    //Add acceleration to speed, make sure it's not above topSpeed)
    speed += Time.deltaTime * acceleration;

    //Set object velocity
    rb.velocity = -transform.forward * speed;
}

The important thing is that you’re updating the speed value every frame in the Update() function by a new parameter: acceleration.

@mptp
Thank you so much! This looks great.

I am getting Error CS0029 cannot implicitly convert type ‘float’ to ‘UnityEngine.Vector3’ on the line " rb.velocity = speed; ".

I’m sure it is because .‘velocity’ is recognized as a vector 3 and Unity cannot equate a float to a vector because a vector consists of 2, 3 or 4 floats. So Unity does not know which one to equate. Am I correct? How could I fix this?
Thanks a million