Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 adriandevera · Jul 04, 2015 at 08:43 PM · vector3listcoordinatesvaluecontains

Vector3 List Find Contains Specific X Coordinate Value and Remove

Im trying to make a dynamic grid system with the mouse. Dont worry about the details too much Im just interesting in how to determine if a list of vector3 have X values that dont contain a number then remove that element. But essentially I click and drag the mouse and its like an RTS selection but rather than a 2d rectangle it places a gridobject down and updates depending on the mouse position

SO to place them down I have this:

     void updateGrids(Vector3 start, Vector3 mouse)
     {
             if (start.x < mouse.x) {
                 for (int x = (int) start.x; x <= mouse.x; x++) {
                     if (start.z < mouse.z) {
                         for (int z = (int) start.z; z <= mouse.z; z++) {
                             if (!gridsPos.Contains(new Vector3(x, start.y, z)))
                             {
                                 GameObject obj = findInactiveGrid();
                                 obj.transform.position = new Vector3(x, start.y, z);
                                 gridsPos.Add(new Vector3(x, start.y, z));
                             }
                         }
 ...... (more etc)

and to remove them if my mouse moves to a different coordinate then remove the grid objects that are not within the rectangular coordinates. So if grid does not contain the mouseRay origin.x then remove it). Never new how to use Lists until lately so Im struggling. I know this would be easier with a dictionary with Gameobjects keys and vector3 position as values but I dont know how to use it completely yet

     void UpdateRemove(Vector3 start, Vector3 mouse)
     {
         if(!gridPos.Contains
 
             gridsPos.con
     }




Comment
Add comment · Show 2
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 NoseKills · Jul 04, 2015 at 10:40 PM 0
Share

So is it actually a grid based game or can objects be placed freely? 2D or 3D ? How many objects do you have on your grid on average ?

List.Contains() loops through the list to check for matches, so it's really slow. Imagine selecting a 30x30 area with 10 units. You could end up in a situation there the 10 units are already added to the list but this code still checks the remaining 890 empty coordinate points against the 10 units in the list. That's almost 10000 checks.

Vector3 x, y and z are floats so I'd say you also risk getting false negatives with if (!gridsPos.Contains(new Vector3(x, start.y, z))).

I don't know what your game is like but 9 times out of 10 it makes more sense to check a list of units to see if they're inside the selection area and mark them as "selected" with a boolean and go from there.

avatar image adriandevera · Jul 05, 2015 at 05:30 AM 0
Share

Its like a tycoon. Its gridbased with rooms and then objects are placed freely. Its 3d spaced, think of it like sims I suppose but its themed.

I have a rounding function that selects the proper tile so rounding is not the issue. Now that I think about it, you are correct this implementation was silly (its good for placing but not searching for deleting grids) so to accommodate I was thinking about using collision testing.

As I drag the mouse, I would modify a dummy trigger box collider. Click down sets the origin and then dragging the mouse would update the verticies accordingly for the bounds. I failed doing this portion earlier. Any help on it via code?

So once thats done I would check objects if they lie within the bounds of the collider, if not then I would do List.Remove for those that do not lie within the 2d box collider. This seems more practical and efficient.

Note: TO clear up, theres no units but NPC sims. The grids are just dummy objects for the projectors to project a grid onto the map itself (:

Heres an image to get a better picture of what Im doing:

http://i.gyazo.com/855c1198d7d2e51f89af5b6e3b632017.gif

So thats working perfectly and Click and dragging works (thanks to the first function i posted updateGrids() but when I move the cursor inwards it doesnt remove the grids out of bounds relative to the mouse. I hope this clears things up

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Checking duplicate position in a list 1 Answer

A node in a childnode? 1 Answer

instantiate problem help? 1 Answer

Vector3[]? 1 Answer

Set Specific List Value 3 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