• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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? 4 Answers

How to make a line between object in Editor Window 1 Answer

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges