Playground Pattern: Player Movement – Auto-Run

This tutorial will show you how to set up auto-running player movement using the Unity Playground scripts. The player will run forwards, but you will be able to adjust their speed and/or their left/right position. If you would like to add jumping, see the jumping tutorial for this.

Previous Tutorials

Step 1 – Create Player GameObject

Create the player’s GameObject by dragging the player’s sprite into the Scene Window. This will create a new GameObject with a SpriteRenderer component attached to it.

Rename the new GameObject “Player” so you don’t get confused later.

Step 2 – Add AutoMove Script

The AutoMove script will propel the player forward at a constant speed without the player having to press any buttons.

Select your new Player GameObject. Add the AutoMove script to the Player, either by dragging the script in from the Project Window and dropping it on the Player in the Hierarchy Window, or by clicking “Add Component” in the Inspector while the Player is selected and browsing to or searching for the “AutoMove” script.

When the AutoMovescript is added, a second component, Rigidbody2D, will also be added. This is a Physics component that the AutoMove script will use to move the Player using Unity’s physics system.

Before moving to the next step, select the Player in the Hierarchy and check the Inspector. Make sure both the AutoMove component and the Rigidbody2D components are attached.

Step 3 – Add Move Script

The Move script will be used to allow the player to move from side to side, and, if desired, to speed up or slow down.

Add the Move script to the player in the same way you added the AutoMove script in Step 2.

Before moving to the next step, select the Player in the Hierarchy and check the Inspector. Make sure the Move component is now attached, in addition to the AutoMove and Rigidbody2D components.

Step 4 – Adjust Settings

Press Play and test the game. You should be able to move the player, but there are some issues with the settings. Make some adjustments as suggested below:

  • Gravity is on. Turn this off in the Rigidbody2D component by setting the gravity scale to 0.
  • Your player accelerates faster and faster and never stops. This is because of Newton’s first law of motion: An object in motion tends to stay in motion. We are adding more and more speed every frame from AutoMove so the player goes faster and faster and never stops. It’s like this in the real world too, but here, we have lots of things slowing us down – friction, air resistance, etc. Set the Rigidbody2D component’s friction to something other than 0 – try 5 and adjust as needed.
  • The player may move too fast or too slow. Try changing the speed on the AutoMove and Move components, and the friction on the Rigidbody2D component, until you get a good balance.
  • The player can speed up and slow down, but you may not want them to. If you don’t want the player to be able to speed up or slow down, change the Move script’s settings to only allow motion in one direction.
  • The player doesn’t move using the correct controls. You can change between WASD or arrow controls as desired.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s