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 Retrogatorade · Aug 08, 2015 at 03:23 PM · colliderinputmousepositiongetmousebuttondown

How to click within a game objects collider?

So I have created a 10x10 grid of squares from quads with their own separate BoxCollider2D and I wanted to, whenever I'm within one of the squares and when it's clicked, to print a statement (any statement would do, it's just to see if it's working).

Here is the code for my Update function:

 void Update () {     
         if (boxColl.bounds.Contains(Input.mousePosition)) {
             if (Input.GetMouseButtonDown (0)) {
                 print ("Hello");
             }
         }
     }

boxColl is a variable that I instantiated earlier to get the component BoxCollider2D.

Now the problem is whenever I click, it doesn't do anything so I was wondering is that because of the read-only property of the Input.mousePosition (because I read the documentation and it returns a Vector3 which you need for the bounds.Contains()) or is it something about colliders and bounds that I don't understand?

Thanks for advance guys!

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 allenallenallen · Aug 08, 2015 at 03:23 PM 1
Share

Use On$$anonymous$$ouseDown? http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseDown.html

avatar image DwaynePritchett · Aug 08, 2015 at 03:25 PM 0
Share

I could be wrong, but I think the mouse position always has a 0 depth value. Basically, it's 2D. I think people have used raycasting in the past. So, Click, and Cast a ray out at mouse x,y,with a Z forward from camera. Then check the collision, if it hits one of your cubes, begin the interacting code. Something along the lines of that.

-Dwayne Pritchett

avatar image Retrogatorade · Aug 08, 2015 at 03:27 PM 0
Share

Wow, I can't believe how simple that was with On$$anonymous$$ouseDown, thanks so much, that works!

@Dwayne, the raycasting sounds cool, I will try that too just for fun, thanks!

avatar image DwaynePritchett · Aug 08, 2015 at 04:21 PM 0
Share

I didn't think it would be as simple as On$$anonymous$$ouseDown either, hm... learn something new everyday.

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

Personaje cambie de sprite al teleportarse [Characters change sprite to teleport] 0 Answers

Jump doesn't work while changing 2d colliders 0 Answers

Input.GetMouseButtonDown only works for 10seconds 2 Answers

What's wrong with OnCollisionEnter? 2 Answers

Synchronizing Fake Mouse Position with Actual Mouse Movement 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