Lesson 2.1 - Player Positioning

Tutorial

·

Beginner

·

+10XP

·

60 mins

·

(2699)

Unity Technologies

Lesson 2.1 - Player Positioning

Overview:

You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food you would like to feed those animals. You will give the player basic side-to-side movement just like you did in Prototype 1, but then you will use if-then statements to keep the Player in bounds.

Project Outcome:

The player will be able to move left and right on the screen based on the user’s left and right key presses, but will not be able to leave the play area on either side.

Languages available:

Overview Video

1. Create a new Project for Prototype 2

The first thing we need to do is create a new project and import the Prototype 2 starter files.

  1. Open Unity Hub and create an empty “Prototype 2” project in your course directory on the correct Unity version.
    If you forget how to do this, refer to the instructions in Lesson 1.1 - Step 1
  2. Click to download the Prototype 2 Starter Files, extract the compressed folder, and then import the .unitypackage into your project.
    If you forget how to do this, refer to the instructions in Lesson 1.1 - Step 2
  3. From the Project window, open the Prototype 2 scene and delete the SampleScene
  4. In the top-right of the Unity Editor, change your Layout from Default to your custom layout

2. Add the Player, Animals, and Food

Let’s get all of our objects positioned in the scene, including the player, animals, and food.

  1. If you want, drag a different material from Course Library > Materials onto the Ground object
  2. Drag 1 Human, 3 Animals, and 1 Food object into the Hierarchy
  3. Rename the character “Player”, then reposition the animals and food so you can see them
  4. Adjust the XYZ scale of the food so you can easily see it from above

3. Get the user’s horizontal input

If we want to move the Player left-to-right, we need a variable tracking the user’s input.

  1. In your Assets folder, create a “Scripts” folder, and a “PlayerController” script inside
  2. Attach the script to the Player and open it
  3. At the top of PlayerController.cs, declare a new public float horizontalInput
  4. In Update(), set horizontalInput = Input.GetAxis(“Horizontal”), then test to make sure it works in the inspector

4. Move the player left-to-right

We have to actually use the horizontal input to translate the Player left and right.

  1. Declare a new public float speed = 10.0f;
  2. In Update(), Translate the player side-to-side based on horizontalInput and speed

5. Keep the player inbounds

We have to prevent the player from going off the side of the screen with an if-then statement.

  1. In Update(), write an if-statement checking if the player’s left X position is less than a certain value
  2. In the if-statement, set the player’s position to its current position, but with a fixed X location

6. Clean up your code and variables

We need to make this work on the right side, too, then clean up our code.

  1. Repeat this process for the right side of the screen
  2. Declare new xRange variable, then replace the hardcoded values with them
  3. Add comments to your code

7. Lesson Recap

New Functionality

  • The player can move left and right based on the user’s left and right key presses
  • The player will not be able to leave the play area on either side

New Concepts & Skills

  • Adjust object scale
  • If-statements
  • Greater/Less than operators

Next Lesson

  • We’ll learn how to create and throw endless amounts of food to feed our animals!

Complete this tutorial