I’m working on prototyping a top-down 2D game (in 3D) and wanted to start off with a bare bones/boilerplate player controller. I found one on the forum that got me up and running quickly, but it had a little extra code and was not using Input.GetAxis or Input.GetButton. It’s recommended to use these rather than Input.GetKey, because it can also apply to game controllers or other input devices. Then you can set the keys in an options screen. I’m planning on testing with an Xbox controller, so I wanted to start it off properly.

Note that the following code is C# (.cs), not JavaScript.

Using Unity’s Character Controller

All you need to do to get your player up and running is:

  1. Create a big world plane/cube and a cube above it.
  2. For top-down, point the camera at the cube from above (in this example, this will be a static camera).  You’ll probably need to rotate the camera’s X by 90 degrees, and move it a bit.
  3. Add the following .cs script to your assets folder.
  4. Add a “Character Controller” component to your cube.
  5. Add the PlayerController script to your cube (you can drag and drop the script into the inspector tab)
  6. Click the Play button!

Save the following as PlayerController.cs

Alternate Approach: Use a Rigid Body

Instead, you could add a rigid body to your cube instead of the Character Controller, and try using rigidbody.AddForce(). Rigid bodies work with Unity’s physics engine. Your character will act like a physics object, with momentum and such. So you may lose some precision and have to program things to counteract it. It’s a choice between programming in your physics and fluidity with a Character Controller, or programming towards more precision with a rigidbody.

Note that you can also add a rigidbody with “isKinematic” checked, to an object with a CharacterController, to have it affect the motion of other rigidbodies.


Update 7/26/2015: Removed the Transform.Translate code, and replaced with code using a Character Controller that should be more helpful as a starting point, and for beginners.

Comments on this Article

  1. Sam Sky says:

    It’s simple and works! Thank you \(^_^)/

  2. Jaami Pavrez says:

    Great it Works ..
    Can you please elaborate this transform.Translate(x,0,y,Space.Self);

  3. KekS says:

    I got a problem with

    ” Transform.Translate(x,0,y,Space.Self); ”

    the error is

    ” Assets/Scripts/player_movement.cs(15,27): error CS0120: An object reference is required to access non-static member `UnityEngine.Transform.Translate(UnityEngine.Vector3)’ ”

    I got the basics of c# but i don´t understand what is wrong.

  4. soulclaimed says:


    im using this for a first person control for a vr island im creating as a project for myself using a bluetooth gamepad. Its great that i now have a way forward backward and sideways/diagonal movement with the gamepad however how can i add jump. Also im trying to get movement in the direction the camera is facing. e.g the camera moves and forward becomes relative to the way the camera is facing. Please can you tell me how to do this its taken me a whole day of google and no luck.

  5. soulclaimed says:

    sorry i should have mentioned that its for android

  6. soulclaimed says:

    im also using google cardboard so cannot use touch controls

  7. Rigid says:

    Rigidbody version:

    using UnityEngine;
    using System.Collections;
    public class PlayerController : MonoBehaviour
        public float speed = 6.0F;
        private Vector3 moveDirection =;
        private Rigidbody controller;
        void Start()
            // Store reference to attached component
            controller = GetComponent();
        void Update()
            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
        void FixedUpdate ()
    • Josh says:

      Great. Thanks for posting.

Leave a Reply

You can use the <pre> tag to post a block of code, or <code> to highlight code within text.