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
1
Question by Benjaben · Apr 28, 2011 at 04:03 PM · raycastrtsselection

Math/Programming Question

I've got the following situation for level editor for the game that I'm building.

http://www.youtube.com/watch?v=PrUuuAOXFuQ

This is the code I have set up for hit.point logic:

void OnGUI() { if(Input.GetMouseButton(0)) { if(drawing == false) { oldMouse = Input.mousePosition; } drawing = true; } if(drawing == true) { DrawRect(oldMouse);

         Ray ray = Camera.main.ScreenPointToRay(oldMouse);
         Ray ray2 = Camera.main.ScreenPointToRay(currMouse);

         if(Physics.Raycast(ray, out hit, 200))
         {

         }

         if(Physics.Raycast(ray2, out hit2, 200))
         {

         }
     }
     if(Input.GetMouseButtonUp(0))
     {
         drawing = false;
     }
 }

 void DrawRect(Vector2 pos)
 {
     Rect rect = new Rect(pos.x, Screen.height - pos.y, Input.mousePosition.x - pos.x, (Screen.height - Input.mousePosition.y) - (Screen.height - pos.y));
     GUI.DrawTexture(rect, tex, ScaleMode.StretchToFill, true);
     currMouse = Input.mousePosition;
 }

The camera can be rotated as well. If there was a way to just pelt the entire area with Raycasts that would be cool haha. I've tried a whole bunch of different things, but none of them are very accurate.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by flaviusxvii · Apr 28, 2011 at 05:04 PM

http://answers.unity3d.com/questions/27319/accurate-rts-selection

Comment
Add comment · Show 2 · 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 kromenak · Apr 28, 2011 at 05:11 PM 0
Share

Haha, this is definitely a better solution :).

avatar image flaviusxvii · Apr 28, 2011 at 06:40 PM 0
Share

Be sure to upvote the original answer by Eric5h5 if it was helpful.

avatar image
1

Answer by kromenak · Apr 28, 2011 at 05:10 PM

You might be able to use Physics.SphereCastAll, but I think that might end up being too inaccurate - what you could really do with is a CubeCastAll or something! Since that's not available, you might be able to perform a number of Physics.RayCastAll at certain intervals in your selection square to get the accuracy you need.

For example, you have the rectangular area between your oldMouse and currMouse. You could cast a 2D array of rays into the scene, at certain intervals from oldMouse to currMouse. You will probably want to use RayCastAll or SphereCastAll, which will return ALL the objects hit by your cast. So, you might get some duplicates, but you can throw those out and feel pretty good about getting accurate results.

Hope that helps!

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

Answer by Ashkan_gc · Apr 28, 2011 at 05:55 PM

the most safe approach is somehow slow. use a capsuleCast and also check the screen space coordinates of objects.

another approach is to check if collider.bound of all objects is inside of a hand crafted bound (using mouse coordinates). it could be inacurate cause sometimes bounds do not match the collider completely.

i think that the rectangle selection can be implemented with any of the methods because %100 acurasy is not really a requirement.

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

Answer by Bunny83 · Apr 28, 2011 at 06:36 PM

You could try to use Rigidbody.SweepTestAll with a Rigidbody + BoxCollider. The screenspace approach is quite good i think but you can only test for certain points (most likely the pivot).

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

RTS Game: Unit selection through Raycast 2 Answers

Unity RayCast Selection 1 Answer

Click and Drag RTS Mouse selecting script help. 1 Answer

Creating a cube as a selection box and detecting all collided objects inside of it. 2 Answers

How do you assign a ray to a game object? 2 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