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 /
avatar image
0
Question by ArthyShow · Aug 06, 2016 at 07:54 PM · 3dspritephysics2dboxcollider2d

Physics2D.OverlapPoint () return always null

Hi everyone, I'm trying to detect mouse click on 2D sprite on a 3D scene.

All my Sprite have a Box Collider 2D (well placed) and a script on it but hit is null all the time. I Also tried to put the Update() function on a script on GameEngine GameObject, but I got the same result.

     void Update () {
       if (Input.GetMouseButtonDown(0)) {
             Vector2 mouse_position = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             Collider2D hit = Physics2D.OverlapPoint (mouse_position);
 
             if (hit) {
                 Debug.Log ("Hit" + hit.transform.name);
             } else {
                 Debug.Log (hit);
             }
         }
     }
 
     void OnMouseDown() {
         Debug.Log ("Hit " + this.name);
     }


alt text

// I tried to update the sprite Z position, but didn't work neither

what.png (143.9 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LoveZelda3 · Dec 08, 2016 at 06:46 AM

If anyone wonders why this function doesn't work with a EdgeCollider2D, as it didn't for me, Unity says on the website: https://docs.unity3d.com/ScriptReference/Collider2D.OverlapPoint.html that it doesn't support it: "This will always return false when used on an EdgeCollider2D.".

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 LoveZelda3 · Dec 07, 2016 at 09:03 PM 0
Share

Now I found that I could use a PolygonCollider2D ins$$anonymous$$d, didn't know what they did before.

avatar image
0

Answer by Spartiate0033 · Dec 07, 2016 at 11:24 PM

Why not make it a UI button? I notice the buildings are under the Canvas, a button would solve all of your issues and allow you to make easy animations/sounds when clicking on it. Really, REALLY easy to do.

The tutorial below is from 4.6, but it still largely functions the same.

https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button

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

Answer by Quasar47 · Oct 04, 2018 at 07:44 AM

I know this thread is a bit old, but for anyone who would come across the same issue, the error is that UIElements are incompatible with OnMouse() functions. Furthermore, BoxColliders will never work on UIElements because the canvas is not set in World space, as such, they are never considered as tangible objects, even with colliders. Instead, UIelements use an Interface called IPointerEnterHandler and IPointerClickHandler ; they are basically used like this :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class MouseOverUI : MonoBehaviour, IPointerEnterHandler, IPointerClickHandler, IPointerDownHandler {
 
  
 
     public void OnPointerEnter(PointerEventData eventData)
     {
         //When the cursor enters a Ui's rect transform. Similar to OnMouseEnter().
     }
 
     public void OnPointerExit(PointerEventData eventData)
     {
         //When the cursor leaves a Ui's rect transform. Similar to OnMouseExit().
     }
 
     public void OnPointerClick(PointerEventData eventData)
     {
       //When the cursor clicks on a Ui's rect transform. Similar to OnMouseDown().
     }
 
 
 
 }
 

The same applies for OnMouseOver and OnMouseUp. Just add their respective interfaces after the keyword "MonoBehaviour" (look up for their documentations if you need them), then you can use their functions. You won't always need the eventData parameter, but always add it in the declaration of your function.

That's all, have a nice day ^^

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.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite sheet sprites into a material 0 Answers

Problem with 2D collision detection (Wide object falls through a thin hole) 1 Answer

[SOLVED] Is it possible to flip a Sprite in 3D if it has a Shader applied? 1 Answer

Sprite collision with Tilemap 2 Answers

Does unity support flatsprite? 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