- Home /
How to debug drawing plane?
I'm kinda new to vector math, I can't figure out how to debug following plane:
Plane playerPlane = new Plane(transform.position+ transform.forward,transform.position);
I understand, that there must be four debug.drawline, but I dont know how to get vector3 for them...
Answer by robertbu · Jun 03, 2013 at 03:20 AM
If you take the cross product of a normal to the plane and any vector that is not colinear (parallel with) with the normal, you will get a vector on the plane. Then you can rotate that vector by 90 degrees using the normal as an axis to other vectors. Here is a bit of code. It draws a square with an X through it on the plane in green and the normal in red. The size of the square is the magnitude of the normal passed in. Planes are infinite, so the square is only to help visualize the plane, not define any kind of boundary.
function DrawPlane(position : Vector3, normal : Vector3) {
var v3 : Vector3;
if (normal.normalized != Vector3.forward)
v3 = Vector3.Cross(normal, Vector3.forward).normalized * normal.magnitude;
else
v3 = Vector3.Cross(normal, Vector3.up).normalized * normal.magnitude;;
var corner0 = position + v3;
var corner2 = position - v3;
var q = Quaternion.AngleAxis(90.0, normal);
v3 = q * v3;
var corner1 = position + v3;
var corner3 = position - v3;
Debug.DrawLine(corner0, corner2, Color.green);
Debug.DrawLine(corner1, corner3, Color.green);
Debug.DrawLine(corner0, corner1, Color.green);
Debug.DrawLine(corner1, corner2, Color.green);
Debug.DrawLine(corner2, corner3, Color.green);
Debug.DrawLine(corner3, corner0, Color.green);
Debug.DrawRay(position, normal, Color.red);
}
There are likely less computationally expensive ways to solve this problem, but this should work fine for debugging.
Hi, I have tried your method, it works. I have a question, in line 4 and 6, what does normal.magnitude effect? The normal size of a plane in unity is 1? So I don't know the function of mormal.magnitude.
Answer by ModelOX · Jun 03 at 07:38 PM
here's a c# version since I find myself coming back to this often
public void DrawPlane(Vector3 position, Vector3 normal)
{
Vector3 v3;
if (normal.normalized != Vector3.forward)
v3 = Vector3.Cross(normal, Vector3.forward).normalized * normal.magnitude;
else
v3 = Vector3.Cross(normal, Vector3.up).normalized * normal.magnitude; ;
var corner0 = position + v3;
var corner2 = position - v3;
var q = Quaternion.AngleAxis(90.0f, normal);
v3 = q * v3;
var corner1 = position + v3;
var corner3 = position - v3;
Debug.DrawLine(corner0, corner2, Color.green);
Debug.DrawLine(corner1, corner3, Color.green);
Debug.DrawLine(corner0, corner1, Color.green);
Debug.DrawLine(corner1, corner2, Color.green);
Debug.DrawLine(corner2, corner3, Color.green);
Debug.DrawLine(corner3, corner0, Color.green);
Debug.DrawRay(position, normal, Color.red);
}
Your answer
Follow this Question
Related Questions
More Vector3 decimal precision in the Console? 3 Answers
DrawLine Is not doing anything. 2 Answers
DrawLine from enemy to player 1 Answer
Why does Debug.DrawLine offset sometimes? 1 Answer
Which Gizmo is Debug.DrawLine? 1 Answer