 Basic movement in Unity3D

How to create Basic Movement in Unity using C# code.

So we got our player Object: And we want to make it move.

How we do it?

Let’s start with simple left and right movement on 2D space. We want to move our character for example on the X axis.

using UnityEngine;

public class PlayerController : MonoBehaviour {

Rigidbody rb;
public float speed = 10.0f;

void Start () {
rb = GetComponent<Rigidbody>();
}

void Update () {
if (Input.GetKey(KeyCode.RightArrow))
{
rb.velocity = transform.right * speed;
}
}
}

Here is the code for moving right if we press the Right Arrow Key. But wait a second! We did move left after the second press while we wanted to move only right!? It’s because after we moved right the ball rotated and the Vector point for moving right was pointed in the other direction.

We can fix it when we add

rb.freezeRotation = true;

to our start function.

So after changing the speed to 2.0f and adding the code above we get something likes this: Looks not that bad. Could be better, but we are just starting.

We want now implement movement to the left. We can just copy our function and change some things so we can move to the left.

using UnityEngine;

public class PlayerController : MonoBehaviour {

Rigidbody rb;
public float speed = 2.0f;

void Start () {
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}

void Update () {
if (Input.GetKey(KeyCode.RightArrow))
{
rb.velocity = transform.right * speed;
}

if (Input.GetKey(KeyCode.LeftArrow))
{
rb.velocity = -transform.right * speed;
}
}
}

There is not such function as transform.left so we need to mirror the transform.right function using the minus symbol. This way we achieved movement left and right.

If we want to add other dimension to our movement and make it really 3D we can add the Z Axis to our code.

Because there is no shortcut for our transform function, we need to call our Vector3 Function directly.

For moving up, we can write something like this:

rb.velocity = new Vector3(0, 0, 1) * speed;

It does the same thing like the one above, but calls additionally Vector3.

And here is the full code:

using UnityEngine;

public class PlayerController : MonoBehaviour {

Rigidbody rb;
public float speed = 2.0f;

void Start () {
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}

void Update () {
if (Input.GetKey(KeyCode.RightArrow))
{
rb.velocity = transform.right * speed;
}

if (Input.GetKey(KeyCode.LeftArrow))
{
rb.velocity = -transform.right * speed;
}

if (Input.GetKey(KeyCode.UpArrow))
{
rb.velocity = new Vector3(0, 0, 1) * speed;
}

if (Input.GetKey(KeyCode.DownArrow))
{
rb.velocity = new Vector3(0, 0, -1) * speed;
}
}
} But is it the only way in which we can move our character? Well… Not really. It’s just the way using the transform function.

If we want to make our ball to move in more realistic way we can do it using the Input.GetAxis() function. This way we are using physics forces which give more realistic feeling.

using UnityEngine;

public class PlayerController : MonoBehaviour {

Rigidbody rb;
public float speed = 2.0f;

void Start () {
rb = GetComponent<Rigidbody>();
}

void Update () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

rb.AddForce(new Vector3(moveHorizontal, 0.0f, moveVertical) * speed);
}
} It’s based on Rigidbody, so changing the mass or Drag in the Rigidbody element through Inspector is going to have impact on our Player Object.

Other way to create player Movement is to give the player Speed with ArrowUp/ArrowDown Key and steer it with ArrowLeft/ArrowRight Key.

using UnityEngine;

public class PlayerController : MonoBehaviour {

public float movementSpeed = 5.0f;
public float rotationSpeed = 200.0f;

void Update () {
transform.Rotate(0, Input.GetAxis("Horizontal") * Time.deltaTime * rotationSpeed, 0);
transform.Translate(0, 0, Input.GetAxis("Vertical") * Time.deltaTime * movementSpeed);
}
} As you see we combined the first two possible solutions for movement and created a new one.

We used the transform function and Input.GetAxis()  to get our new possible movement. Other than that, this type of movement is great for Racing games.

Other than that, we can use the built-in solution which is provided by Unity. The Character Controller: And here is the code for Character Controller which enables us to move our character with WASD or Arrow Keys.

using UnityEngine;

public class PlayerController : MonoBehaviour {

CharacterController characterController;
public float movementSpeed = 5.0f;
private Vector3 moveDirection = Vector3.zero;

void Start()
{
characterController = GetComponent<CharacterController>();

}
void Update () {
if (characterController.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection = moveDirection * movementSpeed;
}

//Gravity
moveDirection.y -= 10f * Time.deltaTime;

characterController.Move(moveDirection * Time.deltaTime);
}
} We don’t use Rigidbody so we need to implement gravitation by ourselves.

That’s all for today. I hope you learned something about Unity Character Movement.

If you want to check more, you can look up on my Jumping tutorial:

How to make the player Object Jump in Unity 3D.

Or the Countdown timer tutorial:

How to create a simple countdown Timer in Unity

This site uses Akismet to reduce spam. Learn how your comment data is processed.