Lab 3 - Establishing Player Control
Tutorial
·
Beginner
·
+10XP
·
60 mins
·
(31)
Unity Technologies

Overview:
In this lesson, you program the player’s basic movement, including the code that limits that movement. Since there are a lot of different ways a player can move, depending on the type of project you’re working on, you will not be given step-by-step instructions on how to do it. In order to do this, you will need to do research, reference other code, and problem-solve when things go wrong. Finally, you will learn about unit testing and start applying unit testing methods to your project.
Project Outcome:
The player will be able to move around based on user input, but not be able to move where they shouldn’t.
Languages available:
1. Introduction
CWC_Intro_lab_3
2. Create PlayerController and plan your code
Regardless of what type of movement your player has, it’ll definitely need a PlayerController script
CWC_Lab3.S1
- Select your Player and add a Rigidbody component (with or without gravity enabled)
- In your Assets folder, create a new “Scripts” folder
- Inside the new “Scripts” folder, create a new “PlayerController” C# script
- Attach it to the player, then open it
- Determine what type of programming will be required for your Player
By the end of this step, you should have a new Script open and a solid plan for what will go in it.
3. Basic movement from user input
The first thing we’ll program is the player’s very basic movement based on user input
CWC_Lab3.S2
- Declare a new private float speed variable
- If using physics, declare a new Rigidbody playerRb variable for it and initialize it in Start()
- If using arrow keys, declare new verticalInput and/or horizontalInput variables
- If basing your movement off a key press, create the if-statement to test for the KeyCode
- Use either the Translate method or AddForce method (if using physics) to move your character
By the end of this step, the player should be able to move the way that you want based on user input.
4. Constrain the Player’s movement
No matter what kind of movement your player has, it needs to be limited for gameplay
CWC_Lab3.S3
- If your player is colliding with objects they shouldn’t (including the ground), check the “Is trigger” box in the Collider component
- If your player’s position or rotation should be constrained, expand the constraints in the Rigidbody component and constrain certain axes
- If your Player can go off the screen, write an if-statement checking and resetting the position
- If the Player can double-jump or fly off-screen, create a boolean variable that limits the user’s ability to do so
- If your player should be constrained by physical barriers along the outside of the play area, create more primitive Planes or Cubes and scale them to form walls
By the end of this step, the player’s movement should be constrained in such a way that makes your game playable.
5. Code Cleanup and Export Backup
Now that we have the basic functionality working, let’s clean up our code and make a backup.
CWC_Lab3.S4
- Create new Empty game objects and nest objects inside them to organize your hierarchy
- Clean up your Update methods by moving the blocks of code into new void functions (e.g. “MovePlayer()” or “ConstrainPlayerPosition()”)
- Add comments to make your code more readable
- Test to make sure everything still works, then save your scene
- Right-click on your Assets folder > Export Package then save a new version in your Backups folder
By the end of this step, your code should be commented, organized, and backed up.
6. Unit Testing, Bug Reporting and Bug Fixing
You learned in the introduction to this pathway that Unit Testing is the first level of software testing. During Unit Testing testers focus on specific units, modules or components of the software to determine whether each one is fully functional. It’s quite common for software developers to perform unit tests before delivering software to testers for formal testing. They consist in testing individual methods and functions of the classes, components or modules used by your software.
As you are still building components of your game that will work eventually integrate and work together continue to test the pieces of your game, track and report bugs, and then fix them, using your database, Unit Test your project so far. Are specific components and pieces of the project working as you specified in your database. If they aren't, start to create bug reports outlining what isn't working - tagging and prioritizing them. Alternatively try and find a fellow learner who can Unit Test your project up to this point and use your database.
It is highly encouraged, that you try to find a fellow learner (using the commenting tools or the Unity Learners Connect group) who would be willing to test and report bugs on your game and vice versa so you truly practice testing and reporting back issue to an external developer and not on your own work.
7. Lesson Recap
CWC_Lab3.SRecap
New progress
- Player can move based on user input
- Player movement is constrained to suit the requirements of the game
- Begin unit testing components of your project
New concepts & skills:
- Program in C# independently
- Troubleshoot issues independently
- Unit Testing