• 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 /
  • Help Room /
avatar image
0
Question by Ploppy3 · Jun 05, 2017 at 09:36 AM · raycastcollider2d

Raycast calls sometime passing through Collider2D's points

Hello, I'm a Unity beginner!

The rays from my Raycast2D calls sometimes pass through points of my PolygonCollider2D and I don't know why.

Hits are marked in the following images by a small yellow horizontal line.

Image: working image: not working

Here is my code:

 Debug.DrawLine(origin, new Vector3(path[i].x, path[i].y, 0), Color.yellow, 0, false);
 float angle = Mathf.Atan2(path[i].y - origin.y,  path[i].x - origin.x);
 Vector2 direction = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
 RaycastHit2D hit = Physics2D.Raycast(origin, direction);
 Debug.DrawLine(new Vector3(hit.point.x, hit.point.y, 0), new Vector3(hit.point.x - .2f, hit.point.y, 0), Color.yellow);
 Debug.DrawRay(new Vector3(origin.x, origin.y, 0), new Vector3(direction.x, direction.y));

Is it due to bad rounding?

If so, how can I fix it?

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 Ploppy3 · Jun 07, 2017 at 02:11 PM

I was told that it was in fact a of float rounding issue.

First solution: Add a "skin" to the objects so that the ray hit the skin and does not miss slightly the object.

Second solution: Offset the ray a tiny bit so that it is sent a little bit into the polygon and not at the exact coordinates of the vertices.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

onMouseUp 2D Issues 0 Answers

hit normal vector from raycast is always [-1,0], which makes slope angle 90 1 Answer

Bounds and extents of a tilemap collider across differing y axes, Tilemap collider bounds 0 Answers

NullReferenceException: Object Reference not set to an instance of an object 0 Answers

How to get my `collider2D.cast` work? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges