• 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 Sgio · Aug 15, 2018 at 12:11 AM · collisionguipuzzlehover

"Collision" between UI images

Hi. Sorry for my bad English. I'll get to the point:

I'm making a puzzle and I decided to do it completely using GUI elements; This is because most of the elements are static (except for the puzzle pieces) and it is not necessary to use physics.

The board consists, among other things, of sockets where the pieces will fit. I want that when a piece passes over a socket (over effect) it changes color. I had intended to use the IPointerEnterHandler interface, but the underlying event would only occur when the mouse pointer enters the socket and by the time this happens, the image of the piece would have entered this space long before. I want that effect to happen immediately after the image that represents the piece invades the space of the socket, not waiting for the pointer to do so. How could I get it? Thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RShields · Aug 15, 2018 at 01:04 AM

There are a few ways to do this, and here's one:

Add a BoxCollider2D (or similar) to all the pieces and sockets, and check "Is Trigger" on all. Then add a RigidBody2D [with Body Type: Kinematic] to each piece. On each socket, add a script like

 private void OnTriggerEnter2D(Collider2D other)
 {
     PieceScript piece = other.GetComponent<PieceScript>();
     if (piece != null)
         piece.TouchingSocket();  // Or whatever function you want
 }

(It actually doesn't matter where the script is [as long as you change it to fit] or where the RigidBody2D is, so long as there's only one script per piece-socket pair and at least one RigidBody2D per piece-socket pair.)

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 Sgio · Aug 15, 2018 at 01:13 AM 0
Share

@RShields Thanks for answering.

Your solution was the first thing that came to $$anonymous$$d and that is what I would have done if I was using sprites ins$$anonymous$$d of UI images. I understand that it is not advisable to use RigidBody2D with elements of the interface.

avatar image RShields Sgio · Aug 15, 2018 at 01:30 AM 0
Share

Some of the other ways involve just doing the math yourself. If you're using circular objects, you can check distance from the picked up piece to every socket to see if they're close enough. If rectangular, check x and y distance. Really, I think you should be fine using colliders. If you're that concerned, it may be worthwhile to consider using sprites ins$$anonymous$$d of UI elements since UI elements aren't really designed to do collision.

avatar image Sgio RShields · Aug 15, 2018 at 02:09 AM 0
Share

Although I favor the use of sprites, if I follow the business logic of the application I intend to develop, "the mere use of UI elements is more convenient". Actually I can think of other ways to solve my problem as you say, but I did not want to use "rudimentary" solutions and I wanted to know through the forum if there were better alternatives. Thank you anyway.

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

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

Related Questions

detect mouseover with grid buttons? 1 Answer

Making a GUI Text Appear on Hover? 1 Answer

Coin pickup script not working..? 1 Answer

Can I use GUIStyle and make the button's textures MovieTextures instead of regular 2D Textures? 0 Answers

Problems with OnTriggerEnter 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