- Home /
Display coordinate system
In the scene view, when an object is selected, the coordinate system is shown. I want the coordinate system can also be shown in the game view. I would appreciate it if you could help me.
Answer by iagoccampos · Sep 06, 2016 at 03:56 PM
Well, you will need a script to do it. Check OnDrawGizmosSelected, the Gizmos class, the DrawLine function and the Color variable.
Answer by LeEHil · Feb 11 at 12:24 PM
I was facing a similar problem and on this post I found the solution. But to clarify the whole "How To" a little better, I want to contribute my solution here:
public void OnDrawGizmosSelected()
{
// draw y axis with arrow
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, (transform.position + (2 * transform.up)));
Cone.DrawCone(transform.position + (2 * transform.up), transform.up, 0.1f);
// draw x axis with arrow
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, (transform.position + (2 * transform.right)));
Cone.DrawCone(transform.position + (2 * transform.right), transform.right, 0.1f);
// draw z axis with arrow
Gizmos.color = Color.blue;
Gizmos.DrawLine(transform.position, (transform.position + (2 * transform.forward)));
Cone.DrawCone(transform.position + (2 * transform.forward), transform.forward, 0.1f);
}
private static class Cone
{
private static Vector3[] newVertices = new Vector3[]
{
new Vector3(-0.0f, 1.0f, 0.0f),
new Vector3(0.2f, 0.0f, 0.4f),
new Vector3(0.5f, 0.0f, 0.0f),
new Vector3(0.0f, 0.0f, 0.0f),
new Vector3(-0.3f, 0.0f, 0.4f),
new Vector3(-0.5f, 0.0f, 0.0f),
new Vector3(-0.2f, 0.0f, -0.4f),
new Vector3(0.2f, 0.0f, -0.4f),
};
private static Vector3[] newNormals = new Vector3[]
{
new Vector3(0,1,0),
new Vector3(1,0,1),
new Vector3(1,0,0),
new Vector3(0,-1,0),
new Vector3(-1,0,1),
new Vector3(-1,0,0),
new Vector3(-1,0,-1),
new Vector3(1,0,-1),
};
private static int[] newTriangles = new int[]
{
0, 1, 2, 2, 1, 3, 0, 4, 1, 1, 4, 3, 0, 5, 4, 4, 5, 3, 0, 6, 5, 5, 6, 3, 0, 7, 6, 6, 7, 3, 0, 2, 7, 7, 2, 3
};
private static Mesh mesh;
static Cone()
{
mesh = new Mesh();
mesh.vertices = newVertices;
mesh.triangles = newTriangles;
mesh.normals = newNormals;
}
public static void DrawCone(Vector3 position, Vector3 rotation, float scale)
{
Gizmos.DrawMesh(mesh, position, Quaternion.FromToRotation(Vector3.up, rotation), new Vector3(scale, 2 * scale, scale));
}
}
Witch this code block you can easily draw a coordinate system. Just attach this to a GameObject and you will have a coordinate system drawn to it's origin using gizmos. To make this coordinate system visible in the game view you will need to enable gizmos there.
Your answer
Follow this Question
Related Questions
Internal coordinates for each object 2 Answers
Coordinates relative to parent (root) 1 Answer
Set camera angle in a script using the same system as the properties page?? 0 Answers
Lost in translation... and by that I mean coordinate systems 1 Answer
My ingame avatar's worldspace does not coincide with other objects 1 Answer