Skip to content
Published October 12, 2020

A debug cross is just what it sounds like. A cross used in debugging!

It takes the familiar shape of the built in move controls, with x,y and z axes visualized in red, green and blue!

It is used to visualize positions in the world.
Want to see where a raycast hit? Just draw a cross!

Now why would you rather have a cross than for example a gizmo box? Well, I personally like the accuracy of the cross. You can see the exact point instead of an aproximation that the box will give.

You have to have Gizmos enabled to be able to see the cross. It can be shown in both scene and game view!

Code wise it is really simple!
The code for creating the cross is using the built-in Debug functionality. What we are doing here is just taking what is already there and putting it in a nicer package, and making it easier to use.

using UnityEngine;
public class DebugCross
{
	public static void DrawCross(Vector3 position, float size = 0.5f, float duration = 2f)
	{
		Debug.DrawLine(position + Vector3.left * size, position + Vector3.right * size, Color.red, duration);

		Debug.DrawLine(position + Vector3.forward * size, position + Vector3.back * size, Color.blue, duration);

		Debug.DrawLine(position + Vector3.up * size, position + Vector3.down * size, Color.green, duration);
	}
}

You can specify where the cross will be displayed, how big it should be, and for how long it should be there. As the DrawCross method is having default values, you can leave both the size and duration if not needing specific values.

The cross is then used like this!

void Update()
	{
		if (Input.GetKeyDown(KeyCode.Space))
		{
			DebugCross.DrawCross(Vector3.zero, 1f, 1f);
		}
	}

Now if you want to get fancy, you can create an extention metod to furter simplify the process of getting the cross to appear! This will make the use extremely straight forward, just slap it onto any existing vector and you’re done!

using UnityEngine;

public static class VectorExtentions
{
	public static void DrawCross(this Vector3 crossPosition, float size = 0.5f, float duration = 2f)
	{
		DebugCross.DrawCross(crossPosition, size, duration);
	}
}

And to use it, do something like this!

void Update()
	{
		if (Input.GetKeyDown(KeyCode.Space))
		{
			Vector3.zero.DrawCross();
		}
	}

Just as with the regular method, you can choose to tweak both the size and duration of the cross (though not needed).

%d bloggers like this: