Unity: Rotation Movement

  • Requirement: Unity
  • Language: C#
  • Difficulty: Easy
  • Written by: Lee Zhi Eng
  • Last update: 31 Aug 2019

Introduction

In our previous tutorial, we have learned how to move a game object around using keyboard input. However, the game object is limited to move along the same directions and it lacks rotation movement. Therefore, in this tutorial, we will learn how to rotate our game object/character using keyboard input.

Rotate Around

Unity provides us with a very easy way to rotate a game object – the RotateAround function. Let’s create a new C# script and attach it to a cube, then add in the following code to its Update function:

void Update()
{
    transform.RotateAround(transform.position, transform.up, 1);
}

If you press the ► Play button now, you should see the cube starts spinning by itself. Let’s talk a little bit about the function used above and how it work. RotateAround is a function under the Transform class which receives 3 input variables:

  • Point – The point in 3D space which serves as the pivot point for the rotation to happen. In this case, we want the cube to rotate around its own center point so we used transform.position.
  • Axis – The axis of which the rotation will happen around it. In this case, we want the cube to rotate around its own y-axis. Sure, we can just use a new Vector3(0, 1, 0) and it may work. However, if the cube rotates around other axis then this will not work as expected. Therefore, we use transform.up instead to make sure it’s always following the game object’s orientation instead of the world orientation.
  • Angle – How much you want the game object to rotate, in degree. In the example above, we rotate the cube 1° in every tick. This means that if we’re running at constant 60fps, our cube will be making a full rotation in just 3 seconds.

Delta Timing & Keyboard Input

Since we have learned what is delta timing and keyboard input in the previous tutorial, we will not repeat it. Let’s integrate our rotation code into the input code:

float rotateSpeed = 100;
if (Input.GetKey(KeyCode.Q))
{
    transform.RotateAround(transform.position, transform.up, -rotateSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.E))
{
    transform.RotateAround(transform.position, transform.up, rotateSpeed * Time.deltaTime);
}

The code above allows us to rotate toward different direction when keyboard button Q or E is pressed. You can also try and rotate the game object toward other directions by changing the axis input variable.

Put Everything Together

Let’s merge our current code with the code from the previous tutorial. This way, you will be able to move the game object more freely around the scene.

Additionally, you can also add a “nose” to the cube to indicate its front direction. You can do so by adding another cube to the main cube and scale it down a little bit. Make sure the main cube is the parent of the smaller cube in the hierarchy window.

The final result looks something like this:

Source Code

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

public class movement : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
    // Update is called once per frame
    void Update()
    {
        // Movement
        float speed = 10;
        Vector3 pos = transform.position;
        if (Input.GetKey(KeyCode.W))
        {
            pos += transform.forward * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.S))
        {
            pos -= transform.forward * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.A))
        {
            pos -= transform.right * speed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.D))
        {
            pos += transform.right * speed * Time.deltaTime;
        }
        transform.position = pos;

        // Rotation
        float rotateSpeed = 100;
        if (Input.GetKey(KeyCode.Q))
        {
            transform.RotateAround(transform.position, transform.up, -rotateSpeed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.E))
        {
            transform.RotateAround(transform.position, transform.up, rotateSpeed * Time.deltaTime);
        }
    }
}

Leave a Reply