Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 alph1 · Apr 30, 2017 at 02:46 PM · collisionphysics2dcollider2dphysics.raycastcollider.raycast

RaycastHit2D problem with PolygonCollider2D

Well i have a polygonCollider2d and i raycast all the points of polygonCollider2D but sometimes it misses the edges of polygoncollider and it doesnt return a point or it just doesnt hit the exact point i set as direction.What i dont understand is when create a direction like (polygonPoint - this.transform.position).normalized shouldnt it collide with the exact point (polygonPoint) of the polygonCollider am i doing something wrong with direction calculation or its just a bug

Comment
Add comment · Show 8
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 Bodrp · Apr 30, 2017 at 04:01 PM 0
Share

Could you add the complete code or some example mimicking the problem you are experiencing?

avatar image alph1 Bodrp · Apr 30, 2017 at 04:11 PM 0
Share

alt text

and this is the code i use to raycast

  Vector2 _dir = (_polygon[i]-this.transform.position).normalized;
     RaycastHit2D _ray = Physics2D.Raycast(this.transform.position,_dir,volumeRange,physical$$anonymous$$ask);

NOTE:volumeRange variable is way bigger than the disance between two points

avatar image Bodrp alph1 · Apr 30, 2017 at 05:16 PM 0
Share

Too bad I can't see the screenshot :(

Could you change your code as such and give back the results (maybe as text ins$$anonymous$$d of screenshot)?

 Vector2 _dir = (_polygon[i]-this.transform.position).normalized;
      RaycastHit2D _ray = Physics2D.Raycast(this.transform.position,_dir,volumeRange,physical$$anonymous$$ask);
      // some new debug code
      bool hasBeenTouched = _ray.transform == this;
      Debug.LogFormat("Point {0} at {1} has been touched: {2}", i, _polygon[i], hasBeenTouched);
      // new code ends here

Also, adding this before the loop and including the result would help:

Debug.Log(this.transform.position);

I think I have an idea of what's going on, but I can't be sure yet.

Show more comments
avatar image alph1 · Apr 30, 2017 at 05:47 PM 0
Share
  (-693.9, -275.8, -7.5) // Debug.Log this.transform
  Point 0 at (-695.9, -279.4) has been touched:False 
  Point 1 at (-695.8, -282.4) has been touched:False
  Point 2 at (-691.8, -282.4) has been touched:False
  Point 3 at (-691.8, -279.4) has been touched:False
 That's the result of your code

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bodrp · May 02, 2017 at 12:14 AM

I reproduced a situation where most of the points targeted by raycast end up having their collider hit, but some don't. Here is the code:

 using System.Collections;
 using UnityEngine;
 using System.Text;
 
 public class RaycastingDemo : MonoBehaviour
 {
     const float ARBITRARY_HIGH_NUMBER = 9000.1f;
 
     [SerializeField] PolygonCollider2D target;
 
     Vector2[] targetPoints;
     Vector2 position;
 
     void Start()
     {
         StringBuilder sb = new StringBuilder();
         position = new Vector2(this.transform.position.x, this.transform.position.y);
         targetPoints = target.points;
         sb.AppendFormat("Raycast origin: {0}\n", position);
 
         for (int i = 0; i < targetPoints.Length; i++)
         {
             Vector2 direction = (targetPoints[i] - position).normalized;
             RaycastHit2D hit = Physics2D.Raycast(this.transform.position, direction, ARBITRARY_HIGH_NUMBER);
             bool targetWasHit = hit.transform == target.transform;
 
             sb.AppendFormat("Point {0} at {1} implies hit: {2}\n", i, targetPoints[i], targetWasHit);
         }
 
         Debug.Log(sb.ToString());
     }
 
     void Update()
     {
         for (int i = 0; i < targetPoints.Length; i++)
         {
             Debug.DrawLine(position, targetPoints[i]);
         }
     }
 }

I put this on a game object as its only component (except its transform) and I put a PolygonCollider2D on another game object, which had a SpriteRenderer. I ended up with a 37 summit polygon.

This is what I saw in scene view after pressing play:

all the raycasts

I wont include all my log, just somes lines:

 Point 32 at (-0.2, -1.1) implies hit: True
 Point 33 at (0.2, -1.1) implies hit: True
 Point 34 at (0.4, -1.0) implies hit: True
 Point 35 at (0.6, -0.9) implies hit: True
 Point 36 at (0.7, -0.8) implies hit: False

Here we see that point 36 was not hit. Point 36, when we look at the picture is that one point where the raycast does not go right through the collider; if it touched, it would be precisely at that point and nowhere else. Another one is in the same situation, point 14, although it is hit.

I tried moving my raycast origin around. Sometimes it hit the edge case, sometimes not.

My guess would be it has to do with float imprecision. If anyone thinks I am wrong, please tell me now and tell me why. Meanwhile, you can read more about float imprecision. It might not help you, but it will help you understand what's happening.


raycasting.png (164.2 kB)
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 alph1 · May 03, 2017 at 12:46 PM 0
Share

Yeah it would be float imprecision,i m going to read your document now but i dont think im going to find a solution for this.You are totally right btw.

avatar image
0

Answer by Glurth · May 01, 2017 at 11:43 PM

From your comment:

 Vector2 _dir = (_polygon[i]-this.transform.position).normalized;

I would be surprised if pologon[i] was a global position (transform.position CERTAINLY is). I suspect pologon[i] is a local position, in model space. To Convert it from model space to world space use Transform.TransformPoint(Vector3) e.g.

 this.transform.TransformPoint(polygon[i]) - this.transform.position

should give you the world-space vector between the polygon point, and the center position of the object.

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 alph1 · May 03, 2017 at 12:48 PM 0
Share

nope all of them are global position i have a function which converts local array to global array.

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

100 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

Related Questions

Ignore collision at the start of the game. 1 Answer

My OnCollisionEnter says i have 128 Collisions every collision 1 Answer

Physics2D.IgnoreCollision Not Working 0 Answers

Physics2D.OverlapCircle dont detect triggers 3 Answers

Get all collision points of a Collider2D.Cast for wall slicing 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