Home > Net >  How to draw a Physics.OverlapBox for debugging (been trying with OnDrawGizmos)?
How to draw a Physics.OverlapBox for debugging (been trying with OnDrawGizmos)?

Time:04-30

So given the position, halfextents and rotation of a Physics.OverlapBox, how would I draw it on the screen for debugging.

If my overlap box was always rotated to Quaternion.identity it would be very easy, I could simply call Gizmos.DrawWireCube with the position and halfextents. However its not correctly orientated. From what I understand I might need to set Gizmos.matrix first, but haven't been able to find anything that works yet.

So given these values for our overlapbox

1) position
2) rotation
3) halfextents

I'm guessing I need these filled in in order to draw it onscreen

Gizmos.matrix = ?
Gizmos.DrawWireCube(?, ?)

I've tried a lot of different values and combinations, but nothing that works. Does anyone know the solution or can set me on the right track? Perhaps I'm on the wrong track entirely. Any advice appreciated.

Edit: note I can draw a gizmos.wirecube to screen and see it, that's no problem. However since I want it correctly orientated, I start to play around with the Gizmos.matrix. After doing this I'm guessing it suddenly disappears way off screen because I can't find it anywhere.

CodePudding user response:

Sounds like you could use e.g. Matrix4x4.TRS

var oldMatrix = Gizmos.matrix;

// create a matrix which translates an object by "position", rotates it by "rotation" and scales it by "halfExtends * 2"
Gizmos.matrix = Matrix4x4.TRS(position, rotation, halfExtends * 2);
// Then use it one a default cube which is not translated nor scaled
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);

Gizmos.matrix = oldMatrix;

As alternative in the case that the position, rotation and scale actually come from a Transform like e.g.

Transform someTransform;
var rotation = someTransform.rotation;
var position = someTransform.position;
var halfExtends = someTransform.lossyScale * 0.5f;

in that case you could also simply use

Gizmos.matrix = someTransform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);

which basically means take this 111 cube like if it where in the local space of given transform and "project" it into world-space using the given matrix.

  • Related