2D rigidbody movement using velocity in Unity

Daniel Kirwan
2 min readAug 31, 2021

--

Objective: Move a player object using the rigid body component in Unity

Moving a player in 2D with the rigid body component is a very simple thing to do.

You need to have a rigidbody component attached to the player, then get the component in code or via a serializedfield.

I chose the serializefield option.

Inside the Update method I call my PlayerMovement method. To be able to move the player with the A or D keys in the horizontal axis you will need to create a float that will be set to the value of the key presses.

The next step is to move the player using the rigid body. You can do this by changing the velocity of the rigid body to a new Vector2 and using the float value * the speed variable as the x-axis value and using the current y value.

This is all you need to move the player using the rigid body in 2D in Unity.

If you chose the serializedfield option, then don’t forget to drag in the rigid body to the correct field in the editor.

And that is it for this article.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet