How to rotate gizmos to fit a game object in Unity

While you are making a cube in unity which is transparent in gameplay, you would probably want to make it visible only in editor mode with gizmos. However, gizmos doesn’t rotate with the object transform. So you can use the following tricks to rotate the gizmos to fit your object. The code shown is to draw a cube which is totally the same as your object. By setting up the matrix of gizmos, we can transform gizmos to the local position, scale and rotation.

Gizmos.color = Color.green;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.zero,Vector3.one);

Advertisements
2 comments
  1. Hello Nicholas, This doesn’t seem to be working for me. The gizmo moves out of alignment when my object is both translated and rotated. Any ideas why I might be having problems with this? Cheers

  2. This works great! thanks dude 🙂

    void OnDrawGizmos () {
    Gizmos.color = Color.yellow;
    Gizmos.matrix = transform.localToWorldMatrix;
    Gizmos.DrawFrustum(transform.position, Camera.main.fieldOfView, Camera.main.nearClipPlane, Camera.main.farClipPlane, Camera.main.aspect);
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: