Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 ArcaneAbyss · May 11, 2018 at 12:43 PM · offsetraycasthit2dboxcollider2d

RaycastHit2D is not precise when casting a ray to a box collider 2d

I am a beginner at unity so bare with me, so when i use a raycasthit2d on a box collider 2d, the raycast activates sometimes outside of the box collider which seems odd and it sometimes does not activate in the box collider. i use debug.drawline to cast a ray to indicate where my ray lands on.

RaycastHit2D collide = Physics2D.Raycast(main.transform.position, Input.GetTouch(i). Debug.Log(collide.collider);

main is the main camera in the scene.

Comment
Add comment · Show 9
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 RobAnthem · May 11, 2018 at 12:59 PM 0
Share

If you are going to raycast using world space AND screen space, you should convert the screen space to world space using Camera.ScreenToWorldPoint(Input.GetTouch(i))

avatar image ArcaneAbyss RobAnthem · May 11, 2018 at 01:13 PM 0
Share

Vector2 hit = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position); RaycastHit2D collide = Physics2D.Raycast(hit, Input.GetTouch(i).position);

Like this?

avatar image RobAnthem ArcaneAbyss · May 11, 2018 at 01:18 PM 0
Share

No, something like this....

 RaycastHit2D collide = Physics2D.Raycast(main.transform.position, Camera.ScreenToWorldPoint(Input.GetTouch(i));
Show more comments
avatar image _Yash_ · May 11, 2018 at 01:46 PM 0
Share

I believe you are raycasting in wrong direction.

 RaycastHit2D collide = Physics2D.Raycast(main.transform.position, Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position));

this will raycast from your camera position to your touch position. The difference is, the ray will not go below your finger ray will be in the X-Y plane and not in Y-Z plane.

try

 origin = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
 direction = Vector3.zero
 
 Physics2D.Raycast( origin, direction );

you can also debug it by debug.drawline ( origin, (origin+Vector3.forward*100)); Also, the camera must be orthographic.

avatar image ArcaneAbyss _Yash_ · May 11, 2018 at 01:58 PM 0
Share

I did the changes so now it is exactly like u have mentioned, however nothing has changed it still return null. I included the debug.drawline, but it did not draw a line for me. Thanks for helping too :D

avatar image _Yash_ ArcaneAbyss · May 11, 2018 at 02:58 PM 0
Share

Well, that is odd, debug.drawline will draw in Scene window just to make sure you are not looking in game view. What is the rotation of your camera? I assumed it is facing positive Z direction.

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

82 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 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 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 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

How can I get my BoxCollider2D to stay centered on my sprite? 2 Answers

Collision Detection + Transform Offset Question 0 Answers

Inaccurency BoxCast2D,Inaccuracy BoxCast2D 0 Answers

2D grab script collider problems 0 Answers

BoxCollider2D only registers clicks in top right quarter of its region 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