Skip to main content

This tutorial is designed to get you up and running with Unity as quickly as possible by showing you how to create a simple player controller system. This player controller and corresponding camera controller system can easily be applied to different types of game player objects such as first person, third person, physics based players, and different types of vehicles.


You may want to skip ahead to the steps and the code but first here is a bit of background behind this method.

Why this System

In learning how to use Unity I encountered some fairly major issues with other examples of basic player controllers.

Firstly I was interested in using physics based players. An example of this is the rolling ball in the Unity Roll a ball tutorial. Other physics based players can include things like flying or rolling vehicles. Physics based movement can be tricky to work with but it also lends itself well to physics based interactions with other physics based objects in the game environment.

However, the first thing that struck me with the Roll a ball example is that there is no fast method for the play to turn around, or to say ‘swivel’, and to then easily move forward and backwards etc in different directions. I also discovered that even if I could get the player to swivel I couldn’t simply parent the camera to the player object in order to face the new direction because if the player object was rolling the camera would inevitably roll with it.

Another reason why I wanted to investigate a relatively simpler method for player movement is because I am interested in developing player controls that can be more easily translated to working with mobile devices that do not normally have a mouse input option. My idea here is that I could create simple UI buttons that could be tapped on mobile devices to control the movement, similar to say the mobile / joystick control systems that you can find in the Unity Asset Store and Youtube.

* Please note, if you are after the more traditional mouse pointer lock style controls that are most commonly used in FPS (first person shooter) games then this system is not for you. In this case you would be much better off following a tutorial like Brackeys’ – First Person Movement.

How This System Works

This system works by creating another ‘Camera Target’ object that is scripted to align with the player’s x, y, z position, but not rotation. The camera is then parented to that object. Controls are then applied to swivel or turn that object which creates the effect of the camera orbiting around that object to face different directions. Physical forces are then applied to player object based on the direction that the camera target object is facing. This is handy in situations where the camera maybe angled differently, so the force is not necessarily applied in the direction that the camera is facing but in the direction that the camera target is facing.

Advantages of This System

The advantage of this system is that you can adjust the position of the camera to either look at your player as in a third person game or be in the position of your player as in a first person game. And, if your player is rotating or spinning around due to rolling or collisions etc this does not necessarily affect the rotation of camera. For a first person controller you can simply turn off the mesh rendering of your player object, in which case the rigid body (or say physical shape) of your player can still interact with the physical environment.

Part 1 – Create a Plane

  1. You should probably start with a new Unity project and call it something like ‘Simple Game Development’ (or similar).
  2. Go to the Game Object main menu and select 3D Object – plane.
  3. With the plane object selected in the Inspector set the scale to 5,5,5. (The position should be 0,0,0). * Because it is a plane object the ‘y’ value doesn’t really do anything, I just change it for the sake of keeping all the values consistent).
  4. Download the checkerboard image and import it into your project assets. Either:
    1. Drag the file from your computer/desktop directly into the assets panel in Unity, or 
    2. Right click in the assets panel and select ‘Import File’.
  5. Drag your checkerboard image from your assets panel onto your plane object.
3D plane object in Unity
3D plane object in Unity.

Part 2 – Create a Sphere / Player Object

  1. Go to the Game Object main menu and select 3D Object – Sphere. 
  2. With the sphere selected use the vertical arrow to lift the sphere above the plane, eg: x=0. y=3, z=0. (*you could also set the y value in the inspector)
  3. Drag your checkerboard image from your assets panel onto your sphere object.
  4. With sphere selected, in inspector Add Component – Rigid Body
  5. Press Play and sphere should drop. *Then don’t forget to deselect Play.
  6. In the hierarchy rename your sphere object to ‘Player’.
Adding the rigid body component to the sphere will cause it to drop and land on the plane when the game is played. It is now a physical object.
Adding the rigid body component to the sphere will cause it to drop and land on the plane when the game is played. It is now a physical object.

Part 3 – Create Bouncy Material

  1. In your assets, right click – Create – Physics Material. Call in ‘Bouncy’. Set the Bounciness to .8.
  2. Select your sphere object and drag your physics material either:
    1. Into the field in the inspector called ‘Material’.
    2. Onto the sphere object.
    3. Onto the inspector panel at the bottom.
  3. Play your game and the ball should bounce.
Creating a bouncy material using Unity
Creating a bouncy material using Unity. This can then be dragged onto objects.

Part 4 – Add a Camera Target Object

Now we are going to add an object that we are going to parent the camera to. When this object is rotated the camera will rotate around that point.

  1. Add a 3cube object
  2. Set the Scale to .25,. 25, .25.
  3. Set the Position to 0, 0.5, 0.
  4. In the Hierarchy rename this cube to ‘Camera Target’.
  5. Select the camera and use the arrows, while looking at the camera view to position the camera how you want it to look at the player. For a fairly standard view you might want to use the position 0, 5, -20.
  6. In the Hierarchy drag the Main Camera object on the ‘Camera Parent’ object to make it a child of the Camera Parent object.
  7. We don’t want this object to collide with anything so in the Inspector uncheck the checkbox for ‘Box Collider’.
  8. * Eventually we will most likely want to make this object invisible by unchecking the box for ‘Mesh Renderer’ but for now it doesn’t hurt to have it visible.

Part 5 – Add Camera Controls

  1. With the Camera Target object selected at the bottom of the inspector click the button to ‘Add Component’, and select New Script, and name it: ‘CameraController’.
  2. Add the following code, making sure the class name matches the name of the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{

    public GameObject player; 
    
    /* 
    You will need to set the player object in Unity. 
    The object that has this script attached to it, ie the 'Camera Target' object
    will then align itself with whatever object is set as the 'Player'
    */

    void Update()
    {
        
        // Align to player position
        transform.position = player.transform.position;
        
        // Rotate left
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(new Vector3(0, -1, 0) * Time.deltaTime * 100, Space.World);
        }
        
        // Rotate right
        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * 100, Space.World);
        }
    }
}
  1. Then with your Camera Target object selected in the Inspector drag the player in the target field.
  2. Now test your game and the camera should orbit around the camera parent object.
Create a camera target object. Don’t forget to deselect the Box Collider.

Part 6 – Add Player Controls

Add a new script to the Player object called ‘BasicPlayerController’:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BasicPlayerController : MonoBehaviour
{

    /* 
    Create a variable called 'rb' that will represent the 
    rigid body of this object.
    */
    private Rigidbody rb;
    
    // Create a public variable for the cameraTarget object
    public GameObject cameraTarget;
    /* 
    You will need to set the cameraTarget object in Unity. 
    The direction this object is facing will be used to determine
    the direction of forces we will apply to our player.
    */

    public float movementIntensity;
    /* 
    Creates a public variable that will be used to set 
    the movement intensity (from within Unity)
    */


    void Start()
    {
    	// make our rb variable equal the rigid body component
        rb = GetComponent<Rigidbody>();
    }

 
    void Update()
    {
    	/* 
    	Establish some directions 
    	based on the cameraTarget object's orientation 
    	*/
        var ForwardDirection = cameraTarget.transform.forward;
        var RightDirection = cameraTarget.transform.right;


        // Move Forwards
        if (Input.GetKey(KeyCode.W)) 
        {
            rb.AddForce (ForwardDirection * movementIntensity);

            /* You may want to try using velocity rather than force.
            This allows for a more responsive control of the movement
            possibly better suited to first person controls, eg: */

            //rb.velocity = ForwardDirection * movementIntensity;
        }

        // Move Backwards
        if (Input.GetKey(KeyCode.S))
        {
            // Adding a negative to the direction reverses it
            rb.AddForce (-ForwardDirection * movementIntensity);
        }

        // Move Rightwards (eg Strafe. *We are using A & D to swivel)
        if (Input.GetKey(KeyCode.E))
        {
           rb.AddForce (RightDirection * movementIntensity);
        }

        // Move Leftwards
        if (Input.GetKey(KeyCode.Q))
        {
           rb.AddForce (-RightDirection * movementIntensity);
        }
    }
}
  1. Play your game and you should be able to control your player and swivel the camera.
  2. If you haven’t already, you may want to uncheck the mesh renderer for the camera parent object.
How to Create a Simple Player Controller Using Unity Tutorial
In this example image I set the colour of the light as well as the background colour to white.

Next Steps

If you haven’t already you might want to trying swapping the force based movement with velocity, as included in the code comments. This is much more responsive than the force based controls.

You may also want to experiment with repositioning the camera and turning the mesh rendering of the player object off to create a really simple first person controller.

A really good thing to do is to add a range of basic 3D objects with rigid bodies that your player can interact with.

You could also see if you can add a vertical lift movement to the controller that could be used for something like a drone, space, or underwater vehicle.

In the next tutorial in this series you will learn how you can extend this basic player controller by adding the ability to jump and move the player only when it is grounded. You’ll also learn how to create a simple projectile system, to shoot stuff 🙂


If you would like to stay in the loop when I publish new tutorials just follow me on either Twitter, LinkedIn, or my Youtube Channel. If you use RSS you can also find feeds for any or all of my tutorial categories.

If you're into game development you may also be interested in some of my articles relating to game design and development.

I would love to make it my thing to provide high quality tutorials and learning guides that are 100% free with no strings attached.

If this tutorial was useful for you please consider showing your appreciation and offering your support. It will make my day!

Buy me a coffee

Filed under:
Game Development

Similar Tutorials:

All Tutorials

Henry Egloff
Twitter Facebook Linked In Instagram Codepen Behance Youtube Settings Letter a