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…
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.
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);
}