Direction vs Rotation - Axis Helper

I’m having a bit of trouble discerning the functional difference between Rotations and Directions. Isn’t a direction just a vector that “points” where the object is aiming (relative to the specified axis)? When should I use a rotation over a direction?

In terms of performance, would it be better to use directions to calculate movements as opposed to rotations? For example:

//Get the direction of my x-axis in world coordinates
Vector3 x = transform.TransformDirection(1, 0, 0);

//Scale 20 meters
x = x * 20;

//Draw a debug line from point to point (along the x-axis in this case)
Debug.DrawLine(transform.position, transform.position + x, Color.red);

I basically was writing a simple helper class to give me feedback from the engine so I could figure out how the heck I got the character controller so messed up. In fact, here it is if anyone else might like it.

It shows you solid and flashing lines from the transform it’s sitting on that indicate the world and local axes (just push the z, y, or z key while in game mode).

I don’t think there’s any bugs, but I can’t say for sure. I’m just really getting started.

using UnityEngine;
using System.Collections;

/// <summary>
/// Drop this on any gameobject to get debug lines showing the world axes or the local axes.  
/// This is handy for newbies (like myself :smile: who are strugglgint to get feedback from the engine
/// while experimenting with the different tools.  I hope it helps someone else.
/// 
/// Author: Maulkye
/// 
/// Usage: This script looks for the x,y, or z keys to be pressed.  If so, it will light up the
/// axis lines for a short period of time.
/// 
/// Notes: The solid red, blue, and green lines represent the world axes and the flashing
/// lines represent the local axes.
/// </summary>
public class AxisHelper : MonoBehaviour 
{

    private int DebugLineDuration = 75; //How many updates beofre lines disappear
    
    //Counters
    private int DebugLinesDuration_X = 0;
    private int DebugLinesDuration_Y = 0;
    private int DebugLinesDuration_Z = 0;
    
    //Working properties
    private Vector3 source, dest;

    private void FixedUpdate()
    {
        
        #region AxisLines

        if (Input.GetKeyDown("x"))
            DebugLinesDuration_X = DebugLineDuration;

        if (DebugLinesDuration_X > 0)
        {
            source = transform.position;
            dest = new Vector3(source.x + 20, source.y, source.z);
            Debug.DrawLine(source, dest, Color.red); //Red for world x-axis

            //Get the direction of my x-axis in world coordinates and scale to 20m
            Vector3 x = transform.TransformDirection(1, 0, 0) * 20;

            //Calculate the point 20m out
            dest = source + x;

            //Draw a debug line from point to point (along the x-axis)
            Color c = Color.white;
            if (DebugLinesDuration_X % 2 == 0)
                c = Color.red;
            
            Debug.DrawLine(source, dest, c);

            if (DebugLinesDuration_X == DebugLineDuration)
            {
                //Place debug outputs here
            }

            DebugLinesDuration_X--;
        }

        if (Input.GetKeyDown("y"))
            DebugLinesDuration_Y = DebugLineDuration;

        if (DebugLinesDuration_Y > 0)
        {
            source = transform.position;
            dest = new Vector3(source.x, source.y + 20, source.z);
            Debug.DrawLine(source, dest, Color.green); //Green for world y-axis

            //Get the direction of my y-axis in world coordinates and scale to 20m
            Vector3 y = transform.TransformDirection(0, 1, 0) * 20;

            //Calculate the point 20m out
            dest = source + y;

            //Draw a debug line from point to point (along the y-axis)
            Color c = Color.white;
            if (DebugLinesDuration_Y % 2 == 0)
                c = Color.green;

            Debug.DrawLine(source, dest, c);

            if (DebugLinesDuration_Y == DebugLineDuration)
            {
                //Place debug outputs here
            }

            DebugLinesDuration_Y--;
        }

        if (Input.GetKeyDown("z"))
            DebugLinesDuration_Z = DebugLineDuration;

        if (DebugLinesDuration_Z > 0)
        {
            source = transform.position;
            dest = new Vector3(source.x, source.y, source.z + 20);
            Debug.DrawLine(source, dest, Color.blue); //Blue for world z-axis

            //Get the direction of my z-axis in world coordinates and scale to 20m
            Vector3 z = transform.TransformDirection(0, 0, 1) * 20;

            //Calculate the point 20m out
            dest = source + z;

            //Draw a debug line from point to point (along the y-axis)
            Color c = Color.white;
            if (DebugLinesDuration_Z % 2 == 0)
                c = Color.blue;

            Debug.DrawLine(source, dest, c);

            if (DebugLinesDuration_Z == DebugLineDuration)
            {
                //Place debug outputs here
            }

            DebugLinesDuration_Z--;
        }

        #endregion

    }
}

Rotations and Directions are convertable into each other it is true. But they have different characteristics when it comes to combining and comparing them.

Depending on how a rotation is expressed, you can add two Rotations and get a new rotation: 90 degrees + 90 degres = 180 degrees. But if you add two direction vectors you get a different vector that doesn’t express a rotation any more.

Rotations (or orientations) and directions are not the same thing, so deciding which to use where is pretty simple: just use a rotation when you need a rotation, and a direction when you need a direction.

A rotation completely describes an object’s absolute orientation (or a change in orientation). A direction (as the term is typically used) just tells you which way an object is pointing. If you think of a spaceship that’s pointing in a given direction, the spaceship can roll about its forward axis without changing its direction. As such, a direction does not uniquely specify an orientation, and direction and orientation are not synonymous.

Again, it really doesn’t matter which is faster, since they do different things. However, any performance differences involved are likely to be unnoticeable, so I really wouldn’t worry about that if I were you.

There’s no need to perform this transformation; the transform class already has a ‘right’ member field that will give you the local x axis for the transform.

This isn’t really accurate (or is maybe just confusingly worded). A direction vector by itself doesn’t represent or specify a rotation, and adding two direction vectors together doesn’t change this (it still won’t specify a rotation).

It is possible to ‘convert’ a direction to a rotation, but since the mapping is not one-to-one (as noted previously), you have to introduce additional constraints in order to arrive at a unique result. (This is basically what functions like the quaternion LookRotation() function do.)

Thank you. This makes more sense now. 8)

Keep in mind that rotations are Quaternions.