• 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
2
Question by zumwalt · Dec 05, 2009 at 08:38 PM · gameobject

multiple object selection

How do I create a way for players to select multiple objects through a square selection. For instance, I want to use Input.mousePosition to get the x,y of the mouse on the screen then of course Input.GetMouseButtonDown to know if element 0 (left mouse button) is down, while this is down, draw a square and stretch it as I have EventType.MouseDrag being true, so while it drags, it creates a larger 2d square on the screen, now the depth from camera would be static so that not all elements in an endless depth are choosen, so say from the camera out to 100meters would be the max depth. Now, ever game object that has a tag that I choose would be selectable and highlights with a circle around it. When I have an EventType.MouseUp, my array of gameobjects would have all of the selected game objects in that array. I can't find anything in the docs for drawing a box on the screen in 2d like I am mentioning, I thought about using a Ray but that is a specific point, using Camera.main.ScreenPointToRay only lets me know if a ray has hit a specific item, this will only be good for single object selection, not multiple.

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

Answer by Bampf · Dec 06, 2009 at 04:43 AM

If you want this to be exact then it may be a lot of work. But it may be fairly simple to get an effect which is very close.

First have a look at the answers to the question, how to tell if a GameObject is seen by a camera. That should give you some ideas.

In particular, the suggestion about using Camera.WorldToViewportPoint may be the best approach. Instead of testing the object against the full viewport, which has coordinates (0,0) to (1,1), you'd be checking against the smaller rectangle that is the area selected with the mouse.

Easiest just to check a single point of each object, probably its center. But if this isn't precise enough you'll have to test more points of each object, and if the objects are complex and rotated arbitrarily it could get tricky. But for most purposes testing just the center point, or points representing the outermost corners of the object, will be good enough.

Edit: In the comments you say "I want to limit the selection to a cubed area". If you really want to do it this way the true area isn't a cube, but rather a truncated pyramid called a frustum. (For details, see "viewing frustum".)

If you were to calculate the 6 planes yourself, you could then use the function GeometricUtility.TestPlanesAABB as follows: You can get the camera's frustum using the function GeometricUtility.CalculateFrustumPlanes. Two of the planes you want are the near and far planes of the camera. The remaining planes you would have to calculate by projecting the four edges of the selected rectangle from the screen into the scene. (All you need are three points to construct each Plane: two adjacent corners of the selected rect, and a point on the ray you get by projecting one of those rays into the scene.)

This is all very doable, particularly if you already understand the geometry, but it's much more involved than my first suggestion. And both ways require you to loop through all the objects and test them. So I think projecting one or more points to the viewport for each object is a simpler solution. And if you test all the points that define the object's bounding volume instead of a single point, it'll be pretty good.

Comment
Add comment · Show 8 · 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 zumwalt · Dec 06, 2009 at 05:14 AM 0
Share

I've already tried the Camera.main.ScreenPointToRay(Input.mousePosition) and seen if the Physics.Raycast(ray) was selected, but then that acted immediately on all objects that the script was attached to, not the results I am looking for, if any part of object is in the selected referenced area, then highlight it, otherwise don't check, so I want to limit the selection to a cubed area based on using the mouse drag event to build a 3d referenced square and then check all gameobjects that are collected in that area and look at their tag, I don't want to do everything in the camera view though

avatar image zumwalt · Dec 06, 2009 at 06:45 AM 0
Share

[code] public class zumSelectObjects : $$anonymous$$onoBehaviour { Vector3 mouseStartPos, mouseEndPos; int counter; void Update() {if (Input.Get$$anonymous$$ouseButtonDown(0)) {mouseStartPos = Input.mousePosition; Debug.Log("Start X" + mouseStartPos.x); StartCoroutine("startCollection");}} IEnumerable startCollection() {counter = 0; while (Input.Get$$anonymous$$ouseButtonDown(0) && counter < 1000) {counter++;} mouseEndPos = Input.mousePosition; Debug.Log("End X:" + mouseEndPos.x); yield return new WaitForSeconds(0);}} [/code] This never prints the end

avatar image Bampf · Dec 06, 2009 at 03:35 PM 0
Share

Note: In response to your comment I added a 2nd method to my earlier answer which works in 3d space.

avatar image zumwalt · Dec 08, 2009 at 06:25 AM 0
Share

Very interesting idea, what I am trying to go for is a simple get x,y of the point of the mouse on the screen, while mouse down, have the method yield until the mouse is up and return that x,y, then in turn have a depth added to make a volume area, not dealing with the near and far plane, but limiting it to a smaller area, then of all the game objects in that area only select those of tag of my choice, your still taking into consideration a very large volume area, I am trying to limit it, but I am not getting the input results from the mouse like I would expect.

avatar image zumwalt · Dec 08, 2009 at 06:28 AM 0
Share

Think Homeworld, how you can select units in a given area on the screen by drawing a box around them, this is using screen to world coordinates which I can't seem to get Unity to respond to, even if my selected area is only a 2 inch square on the screen, the depth would be limited from near plane like you mention to max depth of say 10 meters or 100 meters, etc, not far plane, but going to look into the links you have given to see how they might help.

Show more comments
avatar image
2

Answer by Ashkan_gc · Dec 05, 2009 at 10:54 PM

for drawing a square you should use a texute and scale it or just create a texture procidurally and then draw it. if you want to see if some gameobject is in selection area. you can use GameObject.FindGameobjectsWithTag function to find all of the tagged gameobjects and then compare their transform.position and see if they are in the selected area or not. also you can create a box collider in the selection area and then define a OnTriggerEnter for those tagged gameobjects that is like this.

void OnTriggerEnter (Collider other)
{
if (other.GameObject.Name = "selectionarea")
{
objlist.add (this.gameobject);
}
}

the code is untested and i wrote it here. you should have a static list that has the type in mouse up you can iterate in list with foreach and then get all gameobjects and clear the list in next Mouse down event.

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 zumwalt · Dec 06, 2009 at 02:19 AM 0
Share

find with tag itterates through all game objects, not the ones in an area selected, I use that for other programs, OnTriggerEnter would be for the receiver to see if something entered it, in this case I have a mouse pointer that is with a mouse down and drag that is giving me an area, length with width and assumed depth, so i need to figure out all objects within that area and get a list of all there tags, as i gain the area, I will need to look at the objects tags, its the area with the mouse down that is the problem

avatar image
0

Answer by Drexster · Jul 06, 2011 at 07:49 AM

Hey guys. I'm trying to do something similar but with a 2d line instead of a 3d box. On mouse down i want to collect the objects found underneath the cursor and compare them on mouse up.

I'm currently using a raycast from the main camera with XY coordinates from the mouse to select individual objects on mouse up, but want to be able to select multiple objects in a single mouse down/up.

Any suggestions on how to do this?

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 Chris D · Jul 06, 2011 at 05:05 PM 0
Share

Please don't post new questions in the answer section, especially for a question from 2 years ago.

If you have a related question that isn't answered above, open a new thread and reference this one so people know what you've already looked at.

avatar image Drexster · Jul 06, 2011 at 05:17 PM 0
Share

I'll do that. Thanks!

avatar image
0

Answer by alexfiftyfour1 · Dec 17, 2016 at 11:12 PM

I know, this is an old question, but I had the same problem and I solved it by using colliders.

My selection cube was an empty gameobject with a collider as trigger. The objects that I wanted to select had also colliders. I took the 2D Mouse position, transformed it into world coordinates an sized my cube accordingly. The cube gave me the triggerenter events so I knew when an object was touched by the selection cube.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Adjust the terrain to a mesh 0 Answers

Reading XML file 0 Answers

SetActiveRecursively not activating children 2 Answers

Alternatives to GameObject.Find(); 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