Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Malactus · Feb 13, 2013 at 11:57 PM · mathgizmosgeometrydrawing

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges