- Home /
Basic vector math addition
I feel like I should know this because we went over it in calculus 3 but Im having hardest time getting my vector math to work.
Basically I need to draw a box in the editor based off of this information
public int boundaryLength = 10;
public int boundaryWidth = 10;
public GameObject boundaryStart; // where to start drawing from
I then try to draw the box with the following code
void OnDrawGizmos () { //draw a greenbox around the boundary area if(visualizeGrid == true) { //draw a a green boundary box Vector3 point_1 = boundaryStart.transform.position; Vector3 point_2 = new Vector3(boundaryStart.transform.position.x + boundaryLength,0,0); Vector3 point_3 = new Vector3(boundaryStart.transform.position.x + boundaryLength, 0, boundaryStart.transform.position.z + boundaryWidth); Vector3 point_4 = new Vector3(0, 0, boundaryStart.transform.position.z + boundaryWidth);
Gizmos.color = Color.green;
Gizmos.DrawLine(point_1, point_2);
Gizmos.DrawLine(point_2, point_3);
Gizmos.DrawLine(point_3, point_4);
Gizmos.DrawLine(point_4, point_1);
}
else
{
return; //if visualize gride is turned of, exit function
}
}
This to me seems like it should draw, but I get this weird quadrilateral that is a half box half...not box
Maby my math is wrong but i cant seem to see where
Well I found out that it perfectly works as long as the starting point is exactly at (0,0,0) but when you move the starting point around things get messed up
Are you trying to draw a 2D box around the object in Editor windows? No matter what the camera orientation?
Answer by DaveA · Apr 22, 2011 at 08:20 PM
Vector3 point_4 = new Vector3(0, 0, boundaryStart.transform.position.z + boundaryWidth);
Your x,y for this is always zero. transform.position is in world coords. I think you want some thing more like:
Vector3 point_4 = new Vector3(boundaryStart.transform.position.x, 0, boundaryStart.transform.position.z + boundaryWidth);
In other words, your 'local origin' will be transform.position.x and z, and the offsets will be position.x + length and .z + width.
Your answer
Follow this Question
Related Questions
Angle between two lines in 2D 3 Answers
Average of Vectors [Math] 1 Answer
Rotate 3d vector based on local forward 1 Answer
How to determine if a line intersects with a frustum 2 Answers
Extending a vector? 4 Answers