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 ConfusedExpert · Feb 02, 2015 at 02:15 PM · beginnergridonmousedownoverlap

Detecting OnMouseDown when two objects are overlapping

I'll start off by noting that I am using unity 2d, and all of my GameObjects have sprite renderers. Let's say I have a grid, where each tile is an GameObject with a script (that has a onTileObject field). Let's say I also have some sprites, or characters, that can obviously stand on the tiles.

When someone presses on the tile/character, I'd like for the character's UI to show up, by calling a method in character's script. What's the best way to do it?

Comment
Add comment · Show 6
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 AlwaysSunny · Feb 02, 2015 at 02:10 PM 1
Share

Having a script on every tile seems excessive. Probably shouldn't do that without a good reason. Ins$$anonymous$$d you should store your grid-based world's data in a 2-dimensional array of tiles and access them by their indexers. Then you can look up information about any tile cheaply, such as,

  1. find where mouse clicked

  2. translate into tile-space coordinates

  3. get the tile at that x,y position

  4. search for any actors on that tile

  5. call whatever method is required on the found actor

avatar image ConfusedExpert · Feb 02, 2015 at 02:42 PM 0
Share

Could you go more into detail about how I can achieve

1: find where mouse clicked

and

4: search for any actors on that tile.

I wouldn't know how to do it without a tile GameObject that has a polygon collider and a script with an On$$anonymous$$ouseDown() method.

avatar image AlwaysSunny · Feb 02, 2015 at 03:27 PM 0
Share

I'd wager in just about any tile-based game, the map is created in such a way as to place tiles in a 2D array at some point, such as Tile[,] masterSet; This is extremely advantageous, allowing you, the developer, to access your game world in an intuitive way. If your current method of world generation doesn't do this, it probably should. Guess you could get by without it.

Anyway, knowing where the mouse clicked is as easy as sampling the mouse position on your 2D plane. There's plenty of info around to learn that. The trick is actually step 2, where you round off the mouse position to correspond directly to your Tile[,] array. Then your mouse tile, if it exists, is,

 Tile mouseTile = masterSet[rounded$$anonymous$$ouse.x, rounded$$anonymous$$ouse.y];

In a true old-school tile-based game, any given entity always belonged to one tile at a time. This made it very easy to create a two-way relationship between entities and tiles. So a Tile might have an Entity[] array to hold its entities, and an Entity might store its current tile in a Tile variable. This kind of relationship is also super useful, when it's possible and prudent.

 Entity actor = mouseTile.FirstActorOnSelf();
 if (actor) actor.Perform$$anonymous$$ethod();


avatar image webe · Feb 02, 2015 at 03:39 PM 0
Share

I stored the tiles in a 2d array.

I had clickable towns and units, and wanted to select the unit if a unit and town shared tile, so I first checked the tile for a unit, if no unit was found it checked for a town.

avatar image ConfusedExpert · Feb 02, 2015 at 04:34 PM 1
Share

So, I'm gonna use this chance to ask one more question. AlwaysSunny's solution involves creating a Tile class, which I'm guessing does not extend a monobehaviour. How big is the performance difference between classes that extend monobehaviours and classes that don't? Also, how big is the performance difference between attaching a script to a gameObject and not doing that? Should I be trying to avoid attaching scripts to gameObjects when possible as a rule of thumb?

Also, thank you for the answers, they are really helpful to me.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Saving Grids in Grid System 1 Answer

Move Overlapbox/GameObject depending on Parent 2 Answers

Calling method once 2 Answers

Ground Plane when starting new project 2 Answers

Am I using too many Singletons? 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