Playground Pattern: Player Movement – Spaceship

This tutorial will show you how to set up spaceship-style player movement using the Unity Playground scripts. The Player will be able to rotate their avatar using the keyboard or mouse, then move forwards in the direction they are facing using a set key.

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 Rotate Script

The Rotate script will allow the player to rotate their avatar, changing the direction they are facing.

Select your new Player GameObject. Add the Rotate 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 “Rotate” script.

When the Rotate script is added, a second component, Rigidbody2D, will also be added. This is a Physics component that the Rotate script will use to rotate 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 Rotate component and the Rigidbody2D components are attached.

Step 3 – Add Push Script

The Push script will be used to allow the player to move forward in the direction they are facing when they press a button.

Add the Push script to the player in the same way you added the Rotate script in Step 2.

Before moving to the next step, select the Player in the Hierarchy and check the Inspector. Make sure the Push component is now attached, in addition to the Rotate 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.
  • Once your player moves, they never stop. This is because of Newton’s first law of motion: An object in motion tends to stay in motion. 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 Push and Rotate components, and the friction on the Rigidbody2D component, until you get a good balance.
  • The player doesn’t move using the correct controls. You can change between AD, arrow, or mouse controls for rotation as desired, and can change Push to use whatever button you want.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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