Question is off-topic or not relevant.
[Tutorial] Draw Axis like a 3D modeler (C#)
Hi everyone, I didn't find any post about it, so I share my little script (based on the Unity tutorial) to draw the XYZ axis.
using UnityEngine;
using System.Collections;
public class AxisXYZ : MonoBehaviour
{
static Material lineMaterial;
static void CreateLineMaterial()
{
if (!lineMaterial)
{
// Unity has a built-in shader that is useful for drawing
// simple colored things.
Shader shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 0);
}
}
// Will be called after all regular rendering is done
public void OnRenderObject()
{
CreateLineMaterial();
// Apply the line material
lineMaterial.SetPass(0);
GL.PushMatrix();
// Set transformation matrix for drawing to
// match our transform
GL.MultMatrix(transform.localToWorldMatrix);
// Draw lines
GL.Begin(GL.LINES);
//Draw X axis
GL.Color(Color.red);
GL.Vertex3(0, 0, 0);
GL.Vertex3(10.0f, 0.0f, 0.0f);
//Draw Y axis
GL.Color(Color.green);
GL.Vertex3(0, 0, 0);
GL.Vertex3(0.0f, 10.0f, 0.0f);
//Draw Z axis
GL.Color(Color.blue);
GL.Vertex3(0, 0, 0);
GL.Vertex3(0.0f, 0.0f, 10.0f);
GL.End();
GL.PopMatrix();
}
}
You just need to attach that script to a GameObject placed in the Center of your scene and it's DONE! A way to improve it, it will be to change the size of the lines depending of the size of your object. PEACE! Clément
EDIT : I have improved my system by putting the axis on the right bottom of my screen like every 3D modeler (Unity, 3D's max...). To succeed that, create an empty GameObject (GO). Place it far away from the scene (1000.0f,1000.0f,1000.0f). Inside that GO there is :
A GO with the script above and create an "Axis" layer in Unity Editor.
A Camera
That camera has a small viewport (W 0.25, H 0.25) and it is placed on the bottom right of the screen (X 0.75, Y 0.0)
The Culling mask is only on the layer "Axis" and the Clear FLags is "Depth only".
Place an "Orbital mouse script" on the Main Camera and on "CameraAxis". You can add in this script, like me, a boolean which define if the orbital script is allowed to zoom or not. For the "Main Camera" the scroll is enable. For the "CameraAxis", it's more convenient if the scroll is not enable.
You can see here all my setup :
Tutorials are appreciated, but posting them on Unity Answers as a question seems inappropriate. The forum would probably be more suitable.
Follow this Question
Related Questions
How do I make somthing happen when the Player reaches a certain x, y, z position? 0 Answers
Can someone please help me find out what wrong with my code. 0 Answers
How to make an enemy 2d chasing player? 1 Answer
I can't load play mode 2 Answers
How to set a bool from a script equal to another bool from another script? 1 Answer