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! :)
Answer by mptp · Apr 15, 2016 at 01:39 AM
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.
Answer by stephen_george98 · Apr 15, 2016 at 02:00 PM
@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
$$anonymous$$y apologies! I got myself mixed up!
A Vector3 is more than just a list of floats, in unity it's a whole class, but you're right in that you can't use it as a float.
I've updated my answer to treat speed as a float.
P.s. For the future, this should have been posted as a comment to my answer, rather than as a whiwhole new answer.
Oh okay I gotcha now. Thank you so much for your help @mptp . It works just how I need it to. :)
And I'm sorry I made my response an answer ins$$anonymous$$d of a comment. I clicked on update then this big text box came up so I typed in it. I clicked "reply" on this so hopefully this is a comment, not another answer response. Thanks again! :)