• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gRntaus · Apr 08, 2013 at 01:58 PM · rectcontains

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems with Rect 1 Answer

Best way to check where touched 1 Answer

Rect.Contains in front of gameObject 0 Answers

Just fixed weird mouse over detection problem but not really sure how 1 Answer

Rect contains question (regarding negative x,y values) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges