For this chapter, you will now be shown how you can create a first-person controller within Unity which can be used as the foundation for a future project or your first game.
Set Up
Before we can begin to create our project, we will need to install Unity and create a blank project. You will need to download Unity Hub which is the home for all updates, your projects, and installations of Unity. You can find this download from the Unity website.
2021.3.8f is my recommended version to use, but do ensure that you select which one has been recommended by Unity to use for this tutorial. Once chosen, you will need to select some modules which will support Unity. As I am running a Mac, in the tutorial, you can see that it has been selected for me to have Visual Studio installed as well. This might not appear for you if you have already got Visual Studio installed on your machine. If you decide to use another code editor, then you can untick Visual Studio to be added. You will need to scroll through the list and choose either Mac Build Support or Windows Build Support, depending on the machine you are using. You are now ready to begin the install!
Step 1
Step 2
Step 3
Radius = 0.6
Height = 3.8
Step 4
Step 5
Be sure to then set the Scale to the following so this matches the same size as your First Person Player object. You also need to remove the Capsule Collider as we will be making our own collisions later in the lesson. You can do so by clicking the cog on the far right of the component or by right-clicking the Capsule Collider component and clicking “Remove Component.” You can see this in Figure 4-7.
Scale:
X = 1.2
Y = 1.8
Z = 1.2
Step 6
Step 7
We now need to set up the script which will allow the player to look around. This will work using the X and Y axes and will be locked so they can only look around a certain amount. This is called clamping .
When writing the script, it is handy to know a few things about it. We have begun by setting up some variables which will hold values for our camera, such as mouseSensitivity, which will allow you to change how sensitive the mouse will be when aiming. During our Start Event (void Start), we are locking the cursor , which means that this will not appear in the game while testing/playing. (When running the game and you wish for your cursor to reappear, press Escape on your keyboard.) mouseX and mouseY are both inputs that are built into Unity’s library, which have already been given a button or a gesture. These have been set up to work with the mouse if it were to move up or down, which, in turn, will allow the player to look up and down. And Mathf.Clamp(xRotation, -90f, 90f) is locking how far the player can look up and down. No person can roll their head backward to see behind them, so why should your character do that too?
If completed correctly, you should now have a working script that allows the player to look around the world. Make sure that you test this just to be sure that everything has worked!
Step 8
Just as before, here is a little recap of some of the things we have written about in the script. The CharacterController refers to the component we attached to the Player object at the start of this tutorial and will interact with our code to create movement for our character . The public floats for speed and gravity are default values which can be changed later depending on your game idea. With these variables being made “public,” it allows you to edit these values within the Inspector without the need of opening the movement script if you need to make any changes or adjustments.
Floats X and Z refer to moving the player forward and the direction the player is looking. Just like Mouse X and Mouse Y, these functions are built into Unity’s library and are binded to the arrow keys and WASD. And our Vector3 move = transform.right * x + transform.forward * z refers to our functions multiplying by each other to create movement. Imagine only one of the character’s legs working, neither can work without the other!
Step 9
Step 10
Step 11
Step 12
You can now run your game! This should mean that you now have a fully working first-person controller which can be the start of a new FPS game!
Bonus Stage: Common Unity Terms
Common Unity Terms
Asset Store | A Unity website that lets you download premade assets for use in your game development projects. Unity developers can also publish their own premade assets to the Asset Store for other developers to download and use. |
Bool | A Boolean is a variable which can go two ways, on or off. This can be used to define a situation which is seen as True or False. |
Camera | Orthographic camera A camera view that makes objects appear fixed on the screen, regardless of their actual distance from one another or their relative positions. This is commonly used for retro-style 2D games, as it can make GameObjects look flat, or 2.5D games (2D games that utilize 3D elements), specifically because they allow for touches of 3D depth and definition while maintaining an otherwise 2D appearance. Perspective camera A camera view that projects objects according to their actual placement and distance on screen, giving viewers a sense of their real-world positions. This is commonly used for fully 3D titles. |
Component | Something that is attached to a GameObject to alter its functionality. |
Editor | The dashboard on which all of Unity’s functionality is made available to its users. |
Entity | A GameObject that receives components for functionality. |
Public float | A variable that contains a numeric value stored in a floating point. This can be changed within the Inspector of Unity. |
Private float | A variable that contains a numeric value stored in a floating point. This can only be changed within the script it was created in. |
GameObject | Characters, props, and scenery in Unity. |
Hierarchy window | A window in the Unity Editor that displays all GameObjects currently being used in your Scene. |
Inspector window | The window in which you can view and edit the properties and settings of almost everything that appears in the Unity Editor, including assets, GameObjects, and the Editor itself. |
Instance | A specific version of a GameObject created from a template and modified to carry specific traits and behaviors that differentiate it from its original form. |
Instantiation | The creation of an instance. |
Materials | Editor objects that store the properties of surfaces in Unity , such as texture, shader, and color tint settings. |
Package | A container that holds any combination of Assets, Shaders, Textures, plug-ins, icons, and scripts that enhance various parts of your project. |
Package Manager | A feature within the Unity Editor that allows you to download and install add-ons and enhancements (packages) for the Unity Editor. |
Prefab | A typically customized, reusable version of a GameObject. |
ProBuilder | A Unity feature that enables designers to build, edit, and texture custom 3D geometry for in-scene level design. |
Project window | Effectively the file finder in Unity. This is where you will be able to dive into your Scene, Asset, Prefab, and other folders. |
Rigidbody | A component in Unity that lends a GameObject the ability to react to its environment through physics, for instance, giving a GameObject mass. |
Runtime | The rendered, platform-specific output (e.g., for iOS, Android, Oculus, or PlayStation 4) from a Unity project . |
Scene | The entire editable area in which a game can be built. Environments, props, obstacles, NPCs, menu functions, and more can be a part of each Scene in Unity. |
Shader Graph | A visual Shader editing tool in Unity that lets developers create Shaders without having to write code. |
Timeline | A feature in Unity for creating cinematic content, gameplay sequences, audio sequences, and complex particle effects. |
Time.DeltaTime | This property provides the time between the current and the previous frame. |
UI Elements | A unified UI editing tool in Unity. As of Unity 2020.1, this is known as the UI Toolkit. |
Vector3 | Creates a new vector with given x, y, z components. |
Visual Effect Graph | A node-based visual effect editor that lets developers author visual effects that Unity simulates directly on the GPU. |
Conclusion
- 1.
How to use Unity
- 2.
How do you program your character?
- 3.
Setting up a camera to view your world
With Chapter 4 now completed, we will now begin to look at the designing of your world and a favorite topic of mine, level design. This will look into the fundamentals of “fun” and how to be creative with the levels you intend to make. To help prepare yourself for this chapter, start to think about some games you have played and thought they either had excellent looking graphics or were cleverly designed to suit the story and the gameplay. We will be thinking about a lot of games during this next chapter!