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 Marzoa · Oct 25, 2014 at 05:45 PM · raycastcollidersphysics2dhitboxcollider2d

Some BoxCollider2D don't get hitted by no apparent reason

I have an sprite with several BoxCollider2D attached, it is like a keyboard so each collider is associated to one of the keys. Now, the problem is that I cast a ray when the mouse clicks like:

 RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (e.mousePosition), Vector2.zero);

It works perfectly for the first row, but it always returns null when a collider of the second or third row is clicked. All colliders are equal except for their X e Y coordinates. In fact, I created the first and copied and pasted it to create the rest, just moving each to the right place.

Moreover, to check if it was a problem with the colliders themselves, I disabled the H collider in the first row and put in its place the one that is for the I key in the second row, and then when clicking on such I collider that does not work when it is in the second row, the collision was perfectly detected while it was moved to the first row. Put it in its place on the second row again just changing the X and Y coordinates, and it stop working.

Any ideas?

Thanks a lot in advance.alt textalt text

EDIT: I put a sprite that moves to the mouse coordinates when I click and just have realized there is something wrong with the mouse coords. It is like if the Y-coords were inverted by some reason, and so the first row works well because it is in the middle of the screen, but when I press in the second row, the click is shown above the first row instead of below it.

I have checked the camera is not rotated at all, so I do not know why this is happening. As a work around I can just invert the Y-coord before calling Physics2D.Raycast, but I would prefer to solve it the right way, since it may be a problem for other things later two.

colliders-1.png (136.7 kB)
colliders-2.png (39.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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Oct 25, 2014 at 06:00 PM

You don't show us all the code, but I'll bet that 'e.mousePosition' is from a Event. Event coordinates are GUI coordinates, which are not the same as Screen coordinates. You can use Input.mousePosition instead for the mouse position, or you can convert the GUI mouse position to a Screen mouse position:

 Vector3 v = e.mousePosition;
 v.y = Screen.height - v.y;
 RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (v), Vector2.zero);


 
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 Marzoa · Oct 25, 2014 at 06:19 PM 0
Share

e is Event.current. There is no difference between Event.current.mousePosition.y or Input.mousePosition.y for me. In both cases Y-coords are upside down. The only thing that worked is to invert the Y as I told before, but I was not satisfied with that response because I didn't now why I have to do that. Now I know it, so I did it this way:

 Vector2 screenCoords = new Vector2 (e.mousePosition.x, Screen.height - e.mousePosition.y);

You don't need a Vector3 for that matter, I think. I will mark your answer as accepted, though, since the idea and the result it's the same.

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

2 People are following this question.

avatar image avatar image

Related Questions

RaycastHit Collider Question 1 Answer

Raycast always hit 1 Answer

colliders vs raycast vs physics.overlap? 0 Answers

Why isn't my RaycastHit2D working? 1 Answer

Hit distance Change light range 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