Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by caos23121987 · Jul 17, 2018 at 08:13 AM · collisionuirigidbody2dcollision detectionridgidbody

Ui Collision Detection

Hello.

I would like to show within a canvas element some pictures, which can be moved by drag n` drop. Here, a collision detection to take place, which should prevent the images are placed over each other. I've already tried with Rigidbody2D and BoxCollider2D but unfortunately without success. I hope that who can help. My code currently looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class DragAndRotateHandler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
 {
     private bool drag;
     public void OnDrag( PointerEventData eventData )
     {
         if (drag)
         {
             transform.position = eventData.position;
         }
     }
     public void OnPointerDown( PointerEventData eventData )
     {
         drag = true;
     }
 
     public void OnPointerUp( PointerEventData eventData )
     {
         drag = false;
     }
 
 
     private void OnTriggerEnter( Collider other )
     {
         Debug.Log("collission enter");
     }
     private void OnTriggerEnter2D( Collider2D collision )
     {
         Debug.Log("collission enter 2D");
     }
 
     private void OnTriggerStay( Collider other )
     {
         Debug.Log("collission stay");
     }
     private void OnTriggerStay2D( Collider2D collision )
     {
         Debug.Log("collission stay 2D");
     }
 
     private void OnTriggerExit( Collider other )
     {
         Debug.Log("collission exit");
     }
     private void OnTriggerExit2D( Collider2D collision )
     {
         Debug.Log("collission exit 2D");
     }
 }
 

Thanks for your help

Comment
Add comment · Show 4
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 NoDumbQuestion · Jul 17, 2018 at 09:32 AM 0
Share

I don't think Physics 2D work inside canvas. $$anonymous$$aybe if you change canvas to World space or make sprite ins$$anonymous$$d of UI texture might work

avatar image caos23121987 · Jul 17, 2018 at 10:01 AM 0
Share

He does not display sprites in the canvas. As a space element, unfortunately, I am not there because I need the ui in front of the camera. I am moving in the background about a Large 3D map. Is there another way to detect the collision of 2 images in the canvas? Thank you for your answer.

avatar image NoDumbQuestion caos23121987 · Jul 17, 2018 at 10:54 AM 1
Share

I do this once. But it was much simpler, i just box in the image. Like get 4 corners positioned. If anyone of them get out of border. Call update position that force them go back.

Not optimal but work for simple drag image game

avatar image caos23121987 NoDumbQuestion · Jul 17, 2018 at 11:43 AM 0
Share

$$anonymous$$any Thanks. I'll do it for now until maybe there is a better possibility in Unity. Thank you

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

250 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 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

OnCollisionEnter2D not getting called 2 Answers

How to know when the player object falls between tiles in 2D game to take decesion of gameover 1 Answer

Unable to stop animation upon collision (2d, animation, rigidbody) 0 Answers

Compatible collision constraint methods 0 Answers

Stop objects from going through eachother? 0 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