- Home /
Question by
SpartanDavid · Jul 20, 2021 at 08:10 PM ·
rigidbody2dvelocitytopdownaccelerationrigidbody.addforce
How to get acceleration in a top-down angle
Hello!
I'm currently trying to make a top down project where you can move in all 8 directions. You'd have an acceleration with AddForce and, once you reach the maxSpeed, that would be replaced with a rb.velocity.
I've been trying to reuse a script I did before, but this one was for a 2D platformer, so it only has to check rb.velocity.x, whilst now I want to check that if rb.velocity.x or rb.velocity.y are above maxSpeed, then something else happens. But I can't seem to make it work.
I'm using Input.GetAxisRaw.
My old script was this:
rb.AddForce(new Vector2(moveHor, 0) * acceleration);
if (Mathf.Abs(rb.velocity.x) > maxSpeed)
{
rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x) * maxSpeed, rb.velocity.y);
}
And the one I'm trying to do is this one:
movement = new Vector2(horMove, verMove);
rb.AddForce(movement * acceleration);
if (Mathf.Abs(rb.velocity.magnitude) > maxSpeed)
{
rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x) * maxSpeed, Mathf.Sign(rb.velocity.y) * maxSpeed);
}
Comment