Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by mikembley · Feb 05, 2015 at 02:50 PM · cubelinepointmatrixgizmo

Confusion with Gizmo Matrix when using Gizmo Cube

I'm trying to draw thick lines with Gizmos.DrawCube.

So far i've managed to position the Cube within the line points, yet i'm stumped on how to rotate the gizmo in-place to fit the line points direction.

I know i'm going to have to use Gizmo.matrix, but it ends up offsetting the points.

All i want is the rotation on the center of the gizmo.

alt text

    public static void GizmoDrawLine(Vector2 p1, Vector2 p2, float thickness, Transform t)
     {
         Gizmos.color = Color.red * 0.5f;
         Vector2 direction = p2 - p1;
         Gizmos.DrawCube(p1 + direction * 0.5f, new Vector3(direction.magnitude, thickness, 0f));
     }


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
9
Best Answer

Answer by VesuvianPrime · Feb 05, 2015 at 06:15 PM

You could use something like the following (untested) method:

 public static void DrawCube(Vector3 position, Quaternion rotation, Vector3 scale)
 {
     Matrix4x4 cubeTransform = Matrix4x4.TRS(position, rotation, scale);
     Matrix4x4 oldGizmosMatrix = Gizmos.matrix;
 
     Gixmos.matrix *= cubeTransform;
 
     Gizmos.DrawCube(Vector3.zero, Vector3.one);
 
     Gizmos.matrix = oldGizmosMatrix;
 }
Comment
Add comment · Show 2 · 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 Hypellosh · Nov 11, 2015 at 03:01 PM 0
Share

This code works perfectly! Thanks!!

avatar image Naoweik · Sep 12, 2017 at 01:30 PM 0
Share

You saved my life, thanks !

avatar image
0

Answer by idbrii · Sep 18, 2020 at 10:56 PM

To add to VesuvianPrime's great answer, since you have a Transform, you can use localToWorldMatrix to create your matrix. I think it's the same as Matrix4x4.TRS(transform.position, transform.rotation, transform.lossyScale).

However, you can't just plug that into your current GizmoDrawLine. But you could use it like this to make a frame around an object and follows its rotation:

 public static void GizmoDrawLine(float gap, Transform t)
 {
     var line = 0.07f;
     Gizmos.color = Color.red * 0.5f;
     Gizmos.matrix *= t.localToWorldMatrix;
     var height = new Vector3(line, line + gap * 2f, line);
     var width = new Vector3(line + gap * 2f, line, line);
     Gizmos.DrawCube(Vector3.right * gap, height);
     Gizmos.DrawCube(-Vector3.right * gap, height);
     Gizmos.DrawCube(Vector3.up * gap, width);
     Gizmos.DrawCube(-Vector3.up * gap, width);
 }
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

7 People are following this question.

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

Related Questions

Angle to position on a cube 2 Answers

Find a point at a distance from point on a line passing through two points 1 Answer

Need help fixing an error. Using raycast to mimic laster beam but no collider, Line Renderer, Raycast Collider. 1 Answer

Gizmos.DrawLine - thickens? 5 Answers

How would I replicate Gizmos.DrawLine in-game? 1 Answer


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