Moving the Player

Tutorial

·

Beginner

·

+10XP

·

30 mins

·

(6448)

Unity Technologies

Moving the Player

In the second Roll-a-ball tutorial, you’ll:

  • Add a Rigidbody to the Player sphere, so you can use the Unity physics engine
  • Write your own PlayerController script in C#, to make the sphere respond to player input
  • Test your script and fix errors

Languages available:

1. Overview

In this tutorial, you’ll:

  • Add a Rigidbody to the Player sphere, so you can use the Unity physics engine
  • Write your own PlayerController script in C#, to make the sphere respond to player input
  • Test your script and fix errors

By the end of this tutorial, your game will look something like this:

To use this tutorial, you will need a Unity project with a Scene containing the ball and the surface it rolls on. If you haven’t already created these, start with the tutorial Setting up the Game.

2. Add a Rigidbody to the Player

Follow the video or text instructions below to add a Rigidbody component to the Player GameObject:

Download Transcript

Add a Rigidbody component.

  • Select the Player GameObject in the Hierarchy window.
  • In the Inspector Window, select Add Component, then search for "Rigidbody" and add the Rigidbody component to the Player GameObject.

Note: Make sure to select Rigidbody and not Rigidbody 2D.

3. Add a Player Input component

Follow the video or text instructions below to add the Player Input component:

Download Transcript

1. Add the Player Input component.

  • Select the Player GameObject in the Hierarchy window.
  • In the Inspector window, add the Player Input component.

4. Create a new script

Follow the video or text instructions below to create a new PlayerController script and open it in a script editor:

Download Transcript

1. Create a new PlayerController script.

  • In the Project window, create a new folder named "Scripts".
  • With the Player GameObject selected, select Add Component > New Script, then name the new script "PlayerController".
  • The created script asset will be at the root level of the Assets folder by default. Move the new PlayerController script asset into the Scripts folder.

2. Open the script in a script editor.

  • Double-click the script asset in the Project window to open it in your preferred script editor, usually Visual Studio.

5. Write the OnMove function declaration

Follow the video or text instructions below to delete the Update function, add the InputSystem namespace, and add the OnMove function:

Download Transcript

1. Delete the Update function.

  • If you don’t already have the PlayerController script open, open it now.
  • Delete the entire Update function — you won’t need it.

2. Add the InputSystem namespace.

  • Add the following line of code to the top of the script under the existing namespaces:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

3. Add the OnMove function.

  • Below the Start function but before the final curly brace, add a new line and add the following code:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

6. Apply input data to the Player

Follow the video or text instructions below to set the movement vector, assign a new Rigidbody variable, and add a FixedUpdate function:

Download Transcript

1. Set the movement vector.

  • In the space inside the OnMove function, add the following line of code:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

2. Assign a new Rigidbody variable.

  • Above the Start function, add the following line of code to declare a new variable:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop
  • Next, inside the Start function, add the following line of code to assign the variable’s value:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

3. Add a FixedUpdate function.

  • Create a new function called FixedUpdate below the Start function by adding the following code:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

7. Apply force to the Player

Follow the video or text instructions below to add force to the player, add x and y movement variables, assign those variables, finalize the AddForce call, and test your game.

Download Transcript

1. Add force to the player.

  • In the FixedUpdate function body, add the following code:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop
  • If the Editor shows an error, you can ignore it for now.

2. Add x and y movement variables.

  • Underneath the Rigidbody variable you created earlier, add two more private variables called movementX and movementY of type float:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

3. Assign the movement variables.

  • In the OnMove function, add the following two lines of code:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

4. Finalize the AddForce call.

  • In the FixedUpdate function, add the following new line at the top of the function:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop
  • Revise the second line of code in FixedUpdate to include the movement variable:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

5. Test the game.

  • The Player GameObject should move, but slowly — you’ll fix that in the next step.

8. Fix the Player movement speed

Follow the video or text instructions below to declare a new speed variable, multiply the force by speed, increase the speed in the Inspector window, and test the game:

Download Transcript

1. Declare a new speed variable.

  • Create a new public float variable at the top of the script:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

2. Multiply the force by speed.

  • In the FixedUpdate function, revise the AddForce call to include the speed variable:
[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

3. Increase the speed in the Inspector window.

  • Since the speed is 0, the player won’t move.
  • In the PlayerController component, set the speed value to 10 and test your game.

4. Test the game.

  • The Player GameObject should now move at a more reasonable speed.
  • You can adjust the speed up or down if you want.
Optional Step

9. Final script sample

If your script is not working as expected, below is an example of what your code should look like at this point. The comments have been added to make the code more readable.

PlayerController.cs

[@portabletext/react] Unknown block type "code", specify a component for it in the `components.types` prop

Complete this tutorial