2D Boost in Unity with Rigidbody
In today’s article, I will show you how I implemented a 2D boost mechanic in my space mining game.
First of all, ensure you have a rigidbody 2D component on your game object.
I am using a dodgeForce of 10 and that will be applied to my player object when I press the shift key. You can assign your dodge/boost to whatever button you would like.
private float _dodgeForce = 10f;
In my game I do various things when the player is dodging, for instance, I make sure that they cannot take damage when boosting/dodging for a limited time. But what you want to implement in your boost is up to you. And I also play some boost particles which you can see in the gif at the top.
Vector2 dodgeForce = _movementVector.normalized * _dodgeForce;
// Apply force to the Rigidbody2D component
_rb.AddForce(dodgeForce, ForceMode2D.Impulse);
I calculate the vector that I want to boost along. The _movementVector you see above is the movement vector I use for general movement and is updated constantly by the Unity input system. You should then normalise the vector to give it a length of 1 and then * it by your boost/dodge force.
And thats it. If you apply this to your game you should see the player have a small boost to their movement. If you want it stronger, then just increase the _dodgeForce variable.