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 ragnaros100 · Jul 20, 2012 at 11:41 AM · c#guirtsselectionrectangle

RTS rectangle selection system

I'm trying to make a small RTS-like system where you can select your units and move them around. I was just wondering what the best/easiest method to make a classic rectangle massive selection system? (All it need to do is turn a boolean on the units called 'selected' to true)

-Thanks :)

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

3 Replies

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

Answer by Bunny83 · Jul 21, 2012 at 12:16 PM

It depends on the "style" you want. One way is, when your objects are located in a plane, to use a trigger and colliders on each unit. This could be a bit buggy but you would have a 3D volume in the scene, in other words a true 3d selection area.

The other (and probably better) way is to keep a list of all selectable units (in some kind of gamemanager) and just transform the positions of all units into screenspace. There you can simply use Rect.Contains to test if a unit is inside a rectangle.

edit
I didn't downvote your question, but i guess someone did because it has been asked several times before:

http://answers.unity3d.com/questions/31693/view.html
http://answers.unity3d.com/questions/33901/view.html
http://answers.unity3d.com/questions/168680/view.html

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 ragnaros100 · Jul 21, 2012 at 12:41 PM 0
Share

omg. I never thought of the second option :O ... Thanks man. I'm gonna try that out :)

(if it works, i'll accept your answer :) )

avatar image ragnaros100 · Jul 21, 2012 at 02:07 PM 0
Share

It worked! :D! Thank you so much!

avatar image
1

Answer by Sharpytwo · Jul 21, 2012 at 01:23 PM

well, this isnt really the whole answer, as i do not have it :P
but something that might help you though, is this:

function Update () { var unitPos : Vector3 = Camera.main.WorldToViewportPoint (transform.position); //Debug.Log ("Unit is located at: X:"+ unitPos[0] + "Y:"+ unitPos[1]);

}
attach this to the unit being selected.
it reports the unit position on camera. so if you are able draw a rectangle and calculate if this position is within that rectangle, you are pretty much there. hope this helped somewhat, still working on the same problem myself :-) edit: i see that several people answered just about the same thing before me, but that didnt show when i posted, sorry. :)

Comment
Add comment · Show 1 · 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 ragnaros100 · Jul 21, 2012 at 02:08 PM 0
Share

its okay :) ... Thanks for the answer though! :D

avatar image
1

Answer by mustafa · Jul 21, 2012 at 01:23 PM

I did this before in C#, this is how I did it if it helps:

if(Input.GetMouseButtonDown(0)) { downMousePosition = Input.mousePosition; } else if(Input.GetMouseButtonUp(0)) {

 RaycastHit hit1;
 Physics.Raycast(Camera.main.ScreenPointToRay(downMousePosition), out hit1);
 Vector3 v1 = hit1.point;
 
 RaycastHit hit2;
 Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit2);
 Vector3 v2 = hit2.point;

 Unit[] allUnits = GetAllUnits();

 foreach(Unit unit in allUnits) {
     Vector3 pos = unit.transform.position;
     //is inside the box
     if(Mathf.Max(v1.x, v2.x) >= pos.x && Mathf.Min(v1.x, v2.x) <= pos.x
         && Mathf.Max(v1.z, v2.z) >= pos.z && Mathf.Min(v1.z, v2.z) <= pos.z) {
         //selecte the unit
         Selecte(unit);
     }
 }
 

}

Comment
Add comment · Show 3 · 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 ragnaros100 · Jul 21, 2012 at 02:07 PM 0
Share

omg you guys :D... thanks for all the answers.

avatar image Ziron999 · Sep 17, 2015 at 01:48 AM 0
Share

I recommend using a for ins$$anonymous$$d of a foreach for performance reasons...

avatar image Bunny83 Ziron999 · Sep 17, 2015 at 09:17 AM 0
Share

Since "allUnits" is an array you won't see any preformance differences between for and foreach since it would be implemented as for loop.

Also the for loop is only executed once when you release the mouse button. So this isn't something where preformance plays any role

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

9 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

C# converting Input.Mouseposition.y to to "top" argument of GUI.Box 1 Answer

Dynamic Target for Object field (and others) in Custom Inspector 0 Answers

Selection box - Borders scaling when selection box size grows 1 Answer


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