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
}
}