- Home /
Question by
TheDonkeyQueen · Feb 19, 2014 at 12:43 PM ·
speedacceleration
Acceleration
So I wish to have an object accelerate to a curtain speed limit and hold that speed - stop accelerating once it hits that limit. btw I cannot have a rigidbody attached to this object.
public float minSpeed = 1f;
public float topSpeed = 55f;
int Acceleration = 1;
void Update ()
{
// Movement
transform.position = new Vector2 (transform.position.x + minSpeed * Time.deltaTime, transform.position.y);
How would I do it?
Comment
Answer by Graham-Dunnett · Feb 19, 2014 at 12:44 PM
Use v = v0+0.5at and set a to zero when v gets to 55.
Answer by Bunny83 · Feb 19, 2014 at 12:57 PM
Just to extend what Graham said. You need of course a variable to track your current speed. Something like this:
public float minSpeed = 1f;
public float topSpeed = 55f;
float Acceleration = 1;
float speed = 0f;
void Update ()
{
// apply acceleration
speed += Acceleration * Time.deltaTime;
// clamp the speed between min and max
speed = Mathf.Clamp(speed, minSpeed, maxSpeed);
transform.Translate(speed * Time.deltaTime, 0, 0, Space.World);
}