Acceleration/Deceleration on a Rigidbody2D
Hi all,
I've recently started using Unity after moving over from GameMaker. Unfortunately, I'm having some serious problems with basic movement. I can get my guy to move up, down, left, right (and diagonally), however, I can't get to my character, who uses a rigidbody2d, to accelerate and slow down.
I'm using lerp to try and achieve this but my character simply goes from 0 to maxspeed instantly.`void Update () {
playerMoving = false;
if ((Input.GetAxisRaw ("Horizontal") > 0.5f) || (Input.GetAxisRaw ("Horizontal") < -0.5f)) {
myRigidBody.velocity = new Vector2 ((Mathf.Lerp (0, Input.GetAxisRaw ("Horizontal") * maxSpeed, acceleration)), myRigidBody.velocity.y);
playerMoving = true;
lastMove = new Vector3 (Input.GetAxisRaw ("Horizontal"), 0f, 0f);
}
if ((Input.GetAxisRaw ("Vertical") > 0.5f) || (Input.GetAxisRaw ("Vertical") < -0.5f)) {
myRigidBody.velocity = new Vector2 (myRigidBody.velocity.x, Mathf.Lerp (0, Input.GetAxisRaw ("Vertical") * maxSpeed, acceleration));
playerMoving = true;
lastMove = new Vector3 (0f, Input.GetAxisRaw ("Vertical"), 0f);
}
if (Input.GetAxisRaw ("Horizontal") < 0.5f && Input.GetAxisRaw ("Horizontal") > -0.5f) {
myRigidBody.velocity = new Vector2 (Input.GetAxisRaw ("Horizontal") * acceleration, myRigidBody.velocity.y);
}
if (Input.GetAxisRaw ("Vertical") < 0.5f && Input.GetAxisRaw ("Vertical") > -0.5f) {
myRigidBody.velocity = new Vector2 (myRigidBody.velocity.x, Input.GetAxisRaw ("Vertical") * acceleration);
}`
My understanding is that lerp calculates between point a and point b (in my case, from 0 to the input*maxspeed), and then increments it by the last value, which in my case is acceleration.
I know that my deceleration code at the bottom is wrong because if, for example, the vertical speed between 0.5 and -0.5, it's going to be 0. 0 * acceleration equals 0 but I just have no idea how to decelerate stuff.
Any help would be massively appreciated.