Page cover

Adding the player

Now that we have our bézier curve in place we can now start to create a player, give it the correct components and configure those components.

Creating the player model

First off, we need to create a player that we can use to move along the curve. I've already created a test cube model for this project, so if you want to use that one, feel free to use it and you can skip to Adding components.

We need to create an empty game object and give it a name. I like to name my objects to what they represent so I'll call mine Player. To create an object right-click in the "Hierarchy" then go to "Create empty".

Then right-click on the player object you've just created and got to 3D objects --> Cube.

Note: Make sure you remove the box collider from the newly created 3D cube!

Now let's add two extra cubes to the cube model representing it's eyes.

Note: Just like the previous cube, don't forget to remove the box colliders!

As a final touch lets create some materials for the body and eyes of the player so it will look better and we can better see in which direction the player will be facing.

Simply just right-click into the folder structure then go Create --> Material.

Just give it a name and color, you only have to create a material for the body and for the eyes. If you want to change the color simply press on a material and click on the color display next to "Albedo" and give it any color you like.

Now all you have to do is click on the object you want to change the color of and simply drag and drop the material on it in the inspector.

Adding components

Lets add a box collider to the player. This will be used to detect if the player is entering the collider of the level node later on. For now let adjust the scale a bit so the box collider will be around the area the players feet are. You can leave the size as is, it all depends on your personal preference.

Now I would like to adjust the height and size of the cube so it fits inside the box collider. You can adjust it to whatever you like it to be.

Now let's adjust the Y position of the cube so the bottom of the cube is inside the box collider. For me that would be at 0.7, but yours may vary depending on your scale and size of the box collider and cube.

Next we'll add a rigid body to it as well. This will make sure that the collision between the player and the level node gets triggered. The only thing we have to change here is to uncheck the "Use Gravity" option.

The last component we need to add to the player is the "Player Input" component. The component looks like this.

In the "Actions" box we need to select our Input Action Asset we setup previously. The Default Map will auto-select the action map we've created.

The next thing left for us to do is to change the "Behavior" from "Send Messages" to "Invoke Unity Events".

And with that all are components we need are now added to the player. To save this player as a prefab simply drag the player into the folder structure to create a prefab out of it.

The last thing we need to add to the player is a script that will make the player move across the bézier curve. We will add that now as our next step in this tutorial.

Last updated