- Home /
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
Could you add the complete code or some example mimicking the problem you are experiencing?
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
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.
(-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
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:
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.
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.
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.
nope all of them are global position i have a function which converts local array to global array.