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 noatom · Apr 13, 2014 at 12:33 AM · c#spritepositionclickray

Problems with finding the clicked sprite

First, I know there are functions that automatically get called when a sprite is clicked, but I have to calculate everything from another script which sprite gets clicked.

Anyway, here's the code:

 Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
 RaycastHit hit;
                 
 if( Physics.Raycast( ray, out hit, 100 ) )
 {
 ......................

Basically, it never makes it past that if. What am I doing wrong?

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
Best Answer

Answer by robertbu · Apr 13, 2014 at 01:35 AM

Sprites are 2D. Physics.Raycast() is 3D. You have to use some sort of 2D function to detect the hit on a 2D object. Note Sprites by default do not have a collider, so you will also need to add some sort of collider to your sprite. Then you can do something like:

  var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
  if (hit != null && hit.collider != null) {
     Debug.Log("object clicked: "+hit.collider.tag);
 }

An alternate approach (assuming an orthographic camera) would be to use Camera.ScreenToWorldPoint() and then test the position using Physics2D.OverlapPoint() or Physics2D.OverlapPointNonAlloc().

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 noatom · Apr 13, 2014 at 01:39 AM 0
Share

But shouldn't the 3D version technically work too? Unity does have Z coords even for 2D. And a sprite after all, is just a quad.

avatar image robertbu · Apr 13, 2014 at 02:08 AM 1
Share

@noatom - All raycasting requires a collider. Physics.Raycast() only raycasts against against 3D colliders. Physics2D.Raycast() raycasts only against the 2D colliders. You cannot have a 2D and a 3D collider on the same game object. You could place a box collider or a mesh collider on a child object and then your code would work. Raycasting is all about the colliders and has nothing to do with the mesh. You can put a 2D collider on a complex 3D mesh, or a 3D collider on a sprite. Assu$$anonymous$$g you did not need to handle 2D collisions for your sprite, you could use your code and add a box collider (or any 3D collider) to the game object.

avatar image noatom · Apr 13, 2014 at 08:49 AM 0
Share

ok thanks, got it now.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Help me With clamping gameobjects Codes inside. 1 Answer

Raytrace Ignore the x axis ? Or Am I just a noob ? x') 0 Answers

Change texture every nth second while key down 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