Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 dkfister · Feb 11, 2014 at 12:31 PM · physicsraycastareaselect

Raycasting a specific square area

I'm currently trying to experiment how to do different game types, so I started with the RTS genre. I've got the Select a Single Unit down, with a raycast to the collider of the Unit. But what I need is the way of doing a Square raycast, or something similliar to that, so I can make 2 points, one from MouseButtonDown and Up.

I've tried Physics.OverlapSphere, but this isn't a square area, but rather a Sphere with radius which isn't working in an RTS.

I'm not asking for an super duper solution, i'm just asking how I would go about doing this, since no functions in the Unity API exists for this.

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
1
Best Answer

Answer by CHPedersen · Feb 11, 2014 at 12:52 PM

I'm assuming, by "square raycast", you're talking about selecting multiple units with a square lasso-tool? If so, the trick is to not use raycasting. :) The easiest way is to work solely in screen space. For each of your units, convert their positions to screen space like this:

     Vector3 screenSpacePos = yourCamera.WorldToScreenPoint(yourUnitsTransform.position);

This vector contains the unit's screen (pixel) position in the x and y coordinates. The z coordinate is the unit's depth (distance) from the camera, and is not needed for this.

Record 2 points with the mouse. You have to update the second point in real-time! The first point is where the user started his click, the second point is whatever position the mouse is at in the subsequent frames after the first click:

     // Record this when the user first clicks:
     Vector3 firstPoint = Vector3.zero;
     if(Input.GetMouseButtonDown(0))
         firstPoint = Input.mousePosition;

     // Then record this every frame while the user has the button held down:
     Vector3 secondPoint = Vector3.zero;
     if(Input.GetMouseButton(0))
         secondPoint = Input.mousePosition;

Using these two points, form a rectangle (Rect) and test the unit's screen space position against this Rect using its .Contains method:

     // Every frame, use those two vertices to form a rectangle:
     Rect screenRectAngle = new Rect(firstPoint.x, firstPoint.y, secondPoint.x - firstPoint.x, secondPoint.y - firstPoint.y);

     // Test your unit's screen space position against this rectangle to see if the rectangle contains the point:
     if(screenRectAngle.Contains(screenSpacePos))
         // Your unit is hereby selected...

If Rect.Contains returns true, it's because the unit's screen space position is within the rectangle formed by the mouse during dragging, and the unit should therefore be selected.

Please note that the code above is not presented as a working solution. It is untested and will likely fail if used as is. For example, it completely disregards all the care that must be taken to form the rectangle correctly, if the user moves the mouse's second point behind and/or above the first instead of in front and down. The code sample is intended just to provide an illustration on the approach.

Feel free to ask follow-up questions in the comments. :)

Comment
Add comment · Show 6 · 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
avatar image dkfister · Feb 11, 2014 at 01:23 PM 0
Share

Your code seems to be correct, however I'm not sure if I got the correct way of setting it up correctly. Heres what I did. All the variables seems to have the correct values after some debugging, but it just won't pass through the "contains" test.

http://pastebin.com/hieHRUZt

avatar image CHPedersen · Feb 11, 2014 at 01:37 PM 0
Share

Oh, I bet it's because the Rect expects GUI coords and we're feeding it screen coords, so we have to invert all the y values of the input vectors. Notice the images in the docs: Rect

Try this where you record the firstPoint and secondPoint:

     if (Input.Get$$anonymous$$ouseButtonDown(0))
     {
         firstPoint = Input.mousePosition;
         firstPoint.y = Screen.height - firstPoint.y;
     }

     if (Input.Get$$anonymous$$ouseButton(0))
     {
         secondPoint = Input.mousePosition;
         secondPoint.y = Screen.height - secondPoint.y;
     }

avatar image dkfister · Feb 11, 2014 at 01:46 PM 0
Share

That was it! Works like a charm now (y) I appreciate it mate!

avatar image CHPedersen · Feb 11, 2014 at 01:48 PM 0
Share

No worries. :) Glad you got it working.

avatar image dkfister · Feb 11, 2014 at 01:59 PM 0
Share

Don't know if your still there, but if you are. Could you see if I got this right? I'm trying to make the "selection area" as a GUI so you can actually see the borders you've selected. But it doesn't seem to do anything.

 void OnPostRender() {
         GL.Push$$anonymous$$atrix();
         GL.LoadOrtho();
         GL.Begin(GL.QUADS);
         GL.Color(Color.red);
             GL.Vertex3(firstPoint.x, firstPoint.y, 0);
             GL.Vertex3(firstPoint.x + secondPoint.x, firstPoint.y, 0);
             GL.Vertex3(firstPoint.x + secondPoint.x, firstPoint.y + secondPoint.y, 0);
             GL.Vertex3(firstPoint.x, firstPoint.y + secondPoint.y, 0);
         GL.End();
         GL.Pop$$anonymous$$atrix();
     }
Show more comments

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.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to fill selected area with prefabs? 1 Answer

How do I use layermasks? 9 Answers

Skateboard slope problem 0 Answers

2D Raycast Layer Mask not working 1 Answer

Physics.Raycast (Cheapest Methods) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges