- Home /
Rect.Contains not understanding output?
I'm currently making a marquee selection tool for an RTS which is all but complete I'm just having some funny problems with my box that I can't quite work out. Rect.Contains says that it only checks the X and Y co-ordinates of an object, although my output seems correct the code isn't doing what I am telling it to do, here is the basic code structure.
if(Input.GetMouseButtonUp(0)) {
//If the mouse has been raised and there is a unique start point create and end point
if(start_box != Vector3.zero) {
end_box = Input.mousePosition;
//Bounding box contains the unique start and end points from the selection
//Then creates an array of all selectable objects in the scene
boundbox = new Rect(start_box.x, start_box.y, end_box.x-start_box.x, end_box.y+start_box.y);
selectedunits = GameObject.FindGameObjectsWithTag("Selectable");
for (int i = 0; i < selectedunits.Length; i++) {
Vector3 objectlocation = Camera.main.WorldToScreenPoint(new Vector3(selectedunits[i].transform.position.x,selectedunits[i].transform.position.y,selectedunits[i].transform.position.z));
print ("Box Size: " + boundbox + " Object Location: " + objectlocation);
if(boundbox.Contains(objectlocation)) {
selectedunits[i].SendMessage("setisSelected", true);
}
}
}
}
I also have an on mouse button down that gets the start co-ordinate, these are no problems, my output below is from the print statement of me trying different size boxings only to fail to grab the object......
Box Size: (x:167.00, y:549.00, width:482.00, height:722.00) Object Location: (361.1, 486.6, 138.8)
Box Size: (x:222.00, y:578.00, width:271.00, height:958.00) Object Location: (294.8, 322.0, 101.3)
Box Size: (x:222.00, y:578.00, width:271.00, height:958.00) Object Location: (361.1, 486.6, 138.8)
I think I am messing up my y co-ordinate, probably because I have been looking at it too long, I've tried changing the height of the box to both adding the values together and taking them away. I'm wondering if I need to take the screen size into account when dealing with the y variable or something because I think it's backward. In all cases it seems my x value is fine but my y value falls outside the boxes range even though it shouldn't...
Answer by gRntaus · Apr 08, 2013 at 02:42 PM
Decided I would answer my own question.
I was simply calculating the box wrong and was also not taking into consideration the fact that you can box from multiple directions. Just fixed it up then by creating a method to take care of it for me to keep my code cleaner in the update section, the code now does the following:
private void makeBox() {
float xmin = Mathf.Min(start_box.x, end_box.x);
float ymin = Mathf.Min(start_box.y, end_box.y);
float width = Mathf.Max(start_box.x, end_box.x) - xmin;
float height = Mathf.Max(start_box.y, end_box.y) - ymin;
boundbox = new Rect(xmin, ymin, width, height);
}
Far cleaner and allows me to call it when needed and easily make me the right box.
Your answer
Follow this Question
Related Questions
Rect.Contains Screen Resolution Problem 3 Answers
GUI label dimensions bug 2 Answers
Touch screen coordinates is not same Rect class coordinates? 1 Answer
Problems with Rect 1 Answer
Best way to check where touched 1 Answer