- Home /
Physics2D.Raycast Reflect issue
I know I'm just being an idiot, but I'm pulling my hair out so thought I'd turn to the experts for help.
Quick background, currently working on a script that will project a trajectory however I can't get the raycasting to work properly.
I can cast a ray, have it hit a collider and then cast another ray in the reflected direction, however I can't then reflect another ray.
The below isn't my actual code, but I created a greatly simplified version to work out the issue. If I do the following:
void Update ()
{
ray = new Ray2D (goTransform.position, goTransform.up);
Debug.DrawRay (ray.origin, ray.direction * 50, Color.cyan);
hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity, 1 << LayerMask.NameToLayer("Middleground"));
inDirection = Vector3.Reflect(ray.direction,hit.normal);
Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
Debug.DrawRay (hit.point, inDirection * 100, Color.red);
ray = new Ray2D(hit.point, inDirection);
prevHit = hit.point;
hit = Physics2D.Raycast(hit.point, inDirection, Mathf.Infinity, 1 << LayerMask.NameToLayer("Middleground"));
inDirection = Vector3.Reflect(inDirection, hit.normal);
Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
Debug.DrawRay (hit.point,inDirection * 100, Color.green);
}
The issue is that for the second Ray, it doesn't register a hit.point so every Ray after the first one just uses the same co-ordinates.
Again I'm clearly being an idiot and doing something monumentally stupid, but would really appreciate someone pointing out to me what stupid thing I'm doing/not doing.
Thanks for any help anyone can provide :)
Matt
it would be nice if you can post the actual code or at least something that would work in principle... its hard to tell what could be wrong when the first line of code in the function won't even compile (!)
Answer by robertbu · Jan 09, 2014 at 04:53 PM
I simplified and played a bit with your code. In testing, I found that raycasting from the hit.point sometimes returned the collider that was just hit. Moving the casting point a bit up the normal seems to solve this problem. Here is the code I used that was working correctly for me:
void Update ()
{
Debug.DrawRay (transform.position, transform.right * 50, Color.gray);
RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right, Mathf.Infinity);
if (hit != null && hit.collider != null) {
Vector3 inDirection = Vector3.Reflect(transform.right,hit.normal);
Debug.DrawRay (hit.point, inDirection * 100, Color.red);
hit = Physics2D.Raycast(hit.point+hit.normal*0.01f, inDirection, Mathf.Infinity);
if (hit != null && hit.collider != null) {
inDirection = Vector3.Reflect(inDirection, hit.normal);
Debug.DrawRay (hit.point,inDirection * 100, Color.green);
}
}
}
Note that my original cast was from transform.right rather than transform.up.
You sir are a legend!!
I applied your fix (+hit.normal*0.01f) to my original code and it worked flawlessly first time.
Thank you, thank you, thank you.
On a side note, my original script of some time back used the old Physics.Raycast calls, but as I'm working on a 2D game I thought I'd switch over to the newer code introduced in 4.3.
The original raycast worked perfectly, but the exact same code switched to Physics2D.Raycast (and the associated calls) has the issue I described above. Just wondering if you think this is a bug with the newer 2D features?
Bug or feature, only your application knows for sure. Apparently in 2D, a Raycast from anywhere inside a collider produces a hit. For some things it is a very nice behavior. For example I've seen some clean code using this fact to detect mouse hits on 2D objects. When I saw the behavior of your code I suspected that due to floating point imprecision, the code might detected hit.point as being inside.
I believe this is not a bug within Unity. If the origin of your ray is inside a collier then it will hit it. In your case the origin is exactly on the collider but due to floating point precision errors it probably ends up being inside. You can safely use collision layers (Layer$$anonymous$$ask) for this.
Alternatively, you can maintain the direction and origin of the ray from the first reflection point by adding the (direction*0.01f), rather than moving it up by the normal vector. For example:
Vector2 nextDirection = Vector2.Reflect(firestRay.direction, hit.normal);
Vector2 nextStartPoint = hit.point + nextDirection * .01f;
Ray2D nextRay = new Ray2D(nextStartPoint, nextDirection);
thank you this so usefull but how can use this in game(Not Scence) ? LineRendere ?
Your answer
Follow this Question
Related Questions
Make Ray hit own collider 1 Answer
Balancing Forces + Gravity 1 Answer
Physics2D.Raycasting questions 0 Answers
Held cube clips through wall 0 Answers
IsGrounded Script Not Working 0 Answers