Aside from movement, rotations are also often required in games. Sometimes you want objects to look at another object, or to face a specific direction when they are moving. Here is a small writeup on the most common ways to move objects, both oriented towards 2D and 3D. In case you haven’t read the post about moving an object, you can find it here.

The most common way to move an object in Unity is to set the rotation of an object by accessing gameObject.rotation. Altough you cannot simply set this to an vector. If you want to use vectors to set a gameObject’s rotation you have to use Quaternion.Euler(x,y,z). So in order to make a object look to the right of your screen you would call “this.transform.rotation = Quaternion.Euler(0,-90f,0);”. In case you want to make a object face another object directly you can call “this.transform.LookAt(location)”. In case you want to ease the rotation to gradually start looking at a position you can use “Quaternion.LookRotation(location, up)” and use “Quaternion.Lerp(rotA,rotB,t)”. The easing of objects can often be a hassle to code, because of this I reccomend using a tweening library like DoTween.

Using Rotate() to rotate an object

The rotate method has a lot of use cases, although be mindful that it always adds a rotation. Methodologies without additive rotations can be found next on this page.

All the methods below also allow you to set a space for the rotation.
Worldspace and Localspace.

// Rotate the object based on added angles
public void Rotate(float xAngle, float yAngle, float zAngle)

// Use a vector to add a rotation
public void Rotate(Vector3 eulers)

// Rotate based on an axis, with an added angle
public void Rotate(Vector3 axis, float angle)

using UnityEngine;

public class RotationExamples : MonoBehaviour
{
    [SerializeField] private float degreeStep = 45f;

    [SerializeField] private Transform left;
    [SerializeField] private Transform middle;
    [SerializeField] private Transform right;

    private void Update()
    {
        // Use deltaTime to make the results the same for any
        // framerate. As deltatime is the completion time of a frame.
        float step = degreeStep * Time.deltaTime;

        // Rotating using an angle
        left.Rotate(Vector3.up, step);
        middle.Rotate(Vector3.forward, step);
        right.Rotate(Vector3.right, step);

        // Rotating using vectors
        left.Rotate(Vector3.up * step);
        middle.Rotate(Vector3.forward * step);
        right.Rotate(Vector3.right * step);

        // Rotating using x,y,z
        left.Rotate(0, 1 * step, 0);
        middle.Rotate(0, 0, 1 * step);
        right.Rotate(1 * step, 0, 0);
    }
}
Example of rotating using axis (left: up, mid: forward, right: right)

Setting the rotation directly

The quickest way of directly modifying a rotation of a object is by setting it directly. You can do this by setting “transform.rotation” to a new Quaternion. The quickest way to create a new Quaternion struct is by calling a static function that has been provided by Unity. Called Quaternion.Euler(x,y,z).

using UnityEngine;

public class RotateHorizontally : MonoBehaviour
{
    // This will make it move 90 degrees per second
    [SerializeField] private float speed = 90;
    float yRotation = 0;

    private void Update()
    {
        // Deltatime is the amount of time it takes for a frame to pass
        // This means it is an accurate way of counting upwards.
        yRotation += Time.deltaTime * speed;

        // This will set the rotation to a new rotation based on an increasing Y axis.
        // Which will make it spin horizontally
        this.transform.rotation = Quaternion.Euler(0, yRotation, 0);
    }
}
Example of what the code above does. It rotates the cube 90 degrees horizontally each second. The pause is caused by the video refresh.

Another way of setting the rotation directly is by setting any of the following properties to a directional vector:

transform.rightSetting this aligns the right side of the object to a direction.
transform.up Setting this will make the top “head” of the object align to a direction.
transform.forwardSetting this will make the object face to a specific location.

This makes rotating very easy in general, eliminating the need to tinker with Quaternions. Especially for 2D games. For instance if you want to align a cannon to a target in a game that has Y as up, and X as right. Then you can simply make it aim towards that target by writing “transform.right = targetPosition – cannonPositon;”

using UnityEngine;

public class AimCube : MonoBehaviour
{
    [SerializeField] private Transform target;

    private enum FollowType { Forward, Up, Right }

    // This is seperate for each cube in the inspector, so I can configure them accordingly
    [SerializeField] private FollowType follow;

    private void Update()
    {
        // Get the direction to the target by subtracting the target position by the current position.
        Vector3 directionToTarget = target.transform.position - this.transform.position;

        switch (follow)
        {
            case FollowType.Forward: transform.forward = directionToTarget;
                break;
            case FollowType.Up: transform.up = directionToTarget;
                break;
            case FollowType.Right: transform.right = directionToTarget;
                break;
            default:
                break;
        }
    }
}
You can see three cubes all setting the rotations based on a different axis. In the following order Up, Forward and Right.

Gradually moving the rotation to a target

You can easily ease the transform.forward, transform.up and transform.right to a specific location using “Vector3.MoveTowards(a,b,t)” In the example below I also use “transform.RotateAround(point, axis, amount)” to showcase how the easing towards the target works. In case you still want to

Easing using tranform.forward

using UnityEngine;

public class RotationExamples : MonoBehaviour
{
    [SerializeField] private float followSpeed = 2f;

    [SerializeField] private Transform target;

    private void Update()
    {
        // Get the direction to the target by subtracting the target position by the current position.
        Vector3 directionToTarget = (target.transform.position - this.transform.position).normalized;

        // This is how you move using transforms. Last argument is the steps in Units
        transform.forward = Vector3.MoveTowards(transform.forward, directionToTarget, Time.deltaTime * followSpeed);
       
        // Rotate the target around this object, at a speed of 90 degrees per second.
        target.RotateAround(this.transform.position, transform.up, Time.deltaTime * 90);
    }
}

Easing with Quaternions

using UnityEngine;

public class RotationExamples : MonoBehaviour
{
    [SerializeField] private float degreeStep = 45f;
    [SerializeField] private float followSpeed = 2f;

    [SerializeField] private Transform target;

    private void Update()
    {
        // Get the direction to the target by subtracting the target position by the current position.
        Vector3 directionToTarget = (target.transform.position - this.transform.position).normalized;

            //This is how you do it using quaternions. Last argument is the steps in degrees.
            Quaternion targetRotation = Quaternion.LookRotation(directionToTarget, transform.up);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * degreeStep * followSpeed);

        // Rotate the target around this object, at a speed of 90 degrees per second.
        target.RotateAround(this.transform.position, transform.up, Time.deltaTime * 90);
    }
}
The speed has been lowered to demonstrate that it is rotating to the target in steps