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 Morco · Mar 15, 2014 at 11:54 AM · raycastmousebox collider

Raycast and box collider not working properly

I'm having some weird problems with mouse events, mouse raycast and box collider. Sometimes raycast not hitting the box collider even tho my mouse is right above it. But if i move the mouse to the center of the box, the raycast hits.

This is weird bcuz i was expecting the ray to hit the box at any area of it. Can it be the fact that the z scale of box collider is very small ? Or it depends on the angle of camera ? I use Camera.main.ScreenPointToRay(Input.mousePosition) to get the ray for collision. And same happens of standard OnMouseEnter() it doesnt hit on certent areas of box, close to its margins.

green points raycast hit

red points miss

alt text

Anyone else had this problem or got any idea why this happens ?

untitled.jpg (16.1 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Mar 15, 2014 at 02:29 PM

Don't think it's either of the things you mentioned (cam angle, box thickness.)

ScreenPointToRay completely accounts for camera angle and perspective. It uses the same math that placed the pixels in the first place.

Box thickness can matter in physics, where you might "step through" something thin, but ray casts use math. There's a formula for a line hitting a plane, and so on. But, if you're worried, make it thicker and try.

I'd say do more testing. Move the box and camera around more. Make sure to keep a rendered cube on it (maybe make it 50% transparent, if it blocks to much.) Maybe the box isn't where you think it was. Print what you're hitting. Maybe some other trigger is in the way of that edge. Check the near camera plane in Scene view (shouldn't matter, since the ray starts from there and drawing starts there, so if you can see it you can hit it. But if the box overlaps... .)

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 Goerky · Aug 10, 2021 at 09:38 AM

This Thread is by now over 6 years old, but in case someone stumbles upon this having the same problem (like me), here is the solution:

Layers

Even if you are doing a "2D game" it is still 3D for Unity. If multiple Colliders overlap on the same Depth/ Z-Axis, Unity cannot figure out which one to hit. I dont know why or how it "defaults" to accepting overlaying Colliders within the mentioned circle, but my guess would be their distance to the camera for some form of prioritization.


So the Answer is to give each overlapping Collider's GameObject its own Z-Axis-Value, even if you only work within the 2D-Game's X and Y Dimensions. For Sprites, this problem either not arizes or if it does, you can use the "Sorting Layers" of its GameObject's Sprite Renderer. Make sure to not confuse them with the "Layers" in the top right corner of the Editor - those are for Raycasting. The camera looks towards positive Z per default, meaning that you would increase the Collider's "Layers" (their Z-Axis-position) like 1, 2, 3, ... OR you take a value as upper limit, eg. 10, and work your way back (10 as background, 9 for obstacles, 8, 7, ..., 1)

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

22 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

Related Questions

Prevent raycast to OnMouseOver() or OnMouseEnter() when mouse over UI 1 Answer

Want to move object slowly to where the mouse clicks? 1 Answer

Mouse Over Questions 1 Answer

Instantiating Object On Map 1 Answer

Box collider and Physics.Raycast 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