- Home /
rigidbody2D with instant max speed and slow stop
I've been trying to achieve this for longer than I care to admit...
Here is my current code:
public float maxSpeed = 10f;
private Vector2 movement;
void Update ()
{
float x_move = LeftJoystick.position.x;
float y_move = LeftJoystick.position.y;
movement = new Vector2 (x_move * maxSpeed, y_move * maxSpeed);
}
void FixedUpdate()
{
rigidbody2D.velocity = movement;
}
Using "rigidbody2D.AddForce(movement)" creates an ice skating effect...
I want the character to be able to hit instant top speed and then when I let off the joystick have him slow to a stop. But also be able to instantly change direction when the joystick is pressed again.
Please help!
Answer by BattleBooski · Jan 19, 2014 at 09:51 PM
As always... you figure it out five minutes after asking the question...
My problem was that I was constantly updating the velocity with the joystick's movement; therefore my character was moving based entirely off of that... not the velocity.
Here's the updated code:
void FixedUpdate()
{
if (LeftJoystick.isFingerDown) { // I'm using the C# Joystick class
rigidbody2D.velocity = movement;
}
}
And then use linear drag to adjust the deceleration speed.