- Home /
Gizmos and Drawing a Plane with Rotation
Greetings,
New Unity User here. (and in general to 3D Game Development) After a couple of experimentation with Unity. (mainly involving player interactions with objects, inventory management, etc) I started to dwell in doing a bit more advanced tests.
Recently, I started experimenting with Random Dungeon Generation using predefined Templates which I will create.
In short, there will be a GameObject on the template, which rotation and direction will be used to determine where the next template(s) can be applied to, towards which direction, until no free 'entrance' is available. I know there are its own inherit issues, but I will try to find solutions for them as well :)
So far, I've gotten stuck with Gizmo creation which would make it easier to create such templates.
I might be heading to the wrong direction, But after looking through the Gizmo documentation, I did not find any method to actually draw the gizmo with a set rotation as a plane, for example. So I went ahead and wrote a script which will keep draw a line between 4 points in an area of set-values (in this example, 2x4 units).
The issue is The shape of the door way is not consistent at any angle except for 90 degrees in the X axis. Every other angle will absolutely destroy the shape, but are constraint to a Spherical Influence.
Apologies for my Math, but unforgivably, it is not my strong suite as I'm used to the engines calculating it for me; So I do need some help with this.
Here is the code I have written in C# (a language I'm new to as well, but trying to learn with basis in Java)
using UnityEditor;
using UnityEngine;
using System.Collections;
public class RoomEntityGizmo {
[DrawGizmo (GizmoType.NotSelected | GizmoType.Pickable)]
static void RenderLightGizmo (GameObject obj, GizmoType gizmoType) {
RoomEntry script = (RoomEntry) obj.GetComponent("RoomEntry");
if(script != null)
{
Vector3 position = obj.transform.position;
Quaternion rotation = obj.transform.rotation;
Vector3 forward = obj.transform.forward;
float modifier = 0.5f; // Will Later on increase this if Gizmo is selected.
Gizmos.color = Color.blue * modifier;
Vector3 difference = new Vector3 (script.sizeX,0,script.sizeY);
difference = rotation * difference;
Gizmos.color = Color.yellow * modifier;
Gizmos.DrawLine (
new Vector3( position.x - difference.x, position.y + difference.y, position.z + difference.z ),
new Vector3( position.x + difference.x, position.y + difference.y, position.z + difference.z)
);
Gizmos.DrawLine (
new Vector3(position.x + difference.x, position.y + difference.y, position.z + difference.z),
new Vector3(position.x + difference.x,position.y - difference.y,position.z - difference.z)
);
Gizmos.DrawLine (
new Vector3(position.x + difference.x,position.y - difference.y, position.z - difference.z) ,
new Vector3(position.x - difference.x, position.y - difference.y, position.z - difference.z)
) ;
Gizmos.DrawLine (
new Vector3(position.x - difference.x, position.y - difference.y, position.z - difference.z),
new Vector3(position.x - difference.x, position.y + difference.y, position.z + difference.z)
);
}
}
}
There is a mistake defiantly somewhere here.
Answer by Malactus · Feb 14, 2013 at 12:48 PM
facepalm This is probably why one shouldn't really do Math during night hours :D
After a good rest, It dawned on me that I should probably handle each point individually.
In anycase, If anyone else has this issue, the following is pretty much the jist how I completed this issue: Its also a much shorter solution.
using UnityEditor;
using UnityEngine;
using System.Collections;
public class RoomEntityGizmo {
[DrawGizmo (GizmoType.NotSelected | GizmoType.Pickable)]
static void RenderLightGizmo (GameObject obj, GizmoType gizmoType) {
RoomEntry script = (RoomEntry) obj.GetComponent("RoomEntry");
if(script != null)
{
Gizmos.color = Color.blue;
Gizmos.DrawRay (obj.transform.position, obj.transform.forward);
Vector3[] points = new Vector3[4];
points[0] = drawCubeAndReturnPosition (ref obj, new Vector3( script.sizeX, script.sizeY));
points[1] = drawCubeAndReturnPosition (ref obj, new Vector3( script.sizeX, -script.sizeY));
points[2] = drawCubeAndReturnPosition (ref obj, new Vector3( -script.sizeX, -script.sizeY));
points[3] = drawCubeAndReturnPosition (ref obj, new Vector3( -script.sizeX, script.sizeY));
// Draw lines between points here.
Gizmos.DrawLine (points[0],points[1]);
Gizmos.DrawLine (points[0],points[3]);
Gizmos.DrawLine (points[1],points[2]);
Gizmos.DrawLine (points[2],points[3]);
}
}
private static Vector3 drawCubeAndReturnPosition(ref GameObject obj, Vector3 renderOffset ) {
Vector3 cubeSize = new Vector3(0.05f,0.05f,0.05f);
Vector3 calculatedOffsetPosition = obj.transform.position + obj.transform.rotation * renderOffset;
Gizmos.DrawCube( calculatedOffsetPosition, cubeSize );
return calculatedOffsetPosition;
}
}
This could technically also be applied to a cube with rotations as well.
Answer by nicloay · Sep 02, 2015 at 11:40 AM
This is how I draw plane as gizmo.
void OnDrawGizmos(){
Quaternion rotation = Quaternion.LookRotation(transform.TransformDirection(DragPlaneNormal));
Matrix4x4 trs = Matrix4x4.TRS(transform.TransformPoint(DragPlanePosition), rotation, Vector3.one);
Gizmos.matrix = trs;
Color32 color = Color.blue;
color.a = 125;
Gizmos.color = color;
Gizmos.DrawCube(Vector3.zero, new Vector3(1.0f, 1.0f, 0.0001f));
Gizmos.matrix = Matrix4x4.identity;
Gizmos.color = Color.white;
}
Your answer
Follow this Question
Related Questions
Finding Point on a Bounds (2D Rectangle) using its Centre and an Angle. 1 Answer
3d Line Drawing in editor with z-sorting 2 Answers
SetFromToRotation vs SetLookRotation 2 Answers
How to scale/calculate GameObject Z distance to the real physical world... 0 Answers
Not so much a scripting problem - Just need smart peoples' help. 2 Answers