#Raycasts

1 messages · Page 1 of 1 (latest)

rare jungle
#

Raycasts

rapid monolith
#

Tip: post the updated code where you have fixed these things we pointed out

rare jungle
#

ok, here is the normals screenshot I was requested and I'll send the updated code:

#
        private bool RayCast(Vector2 dir)
        {
            var bouncesLeft = _bounceAmount + 1;
            Vector2 castOrigin = transform.position;
            while (bouncesLeft > 0)
            {
                var hit = Physics2D.Raycast(castOrigin, dir, 5f, _layerMask);
                if (hit.collider == null)
                {
                    Debug.DrawRay(castOrigin, dir * 5);
                    return false;
                }

                Debug.DrawLine(hit.point, hit.point + hit.normal, Color.green);
                Debug.DrawRay(castOrigin, dir * hit.distance);
                if (hit.transform.CompareTag("Player")) return true;
                dir = Vector2.Reflect(dir, hit.normal);
                castOrigin = hit.point;
                bouncesLeft--;
            }
            return false;
        }
rare jungle
# rare jungle

green lines represent normals and white ones represent the raycast

#

so now it works at certain angles but not at others 0_o

rapid monolith
#

hit.collider is always there if hit.transform is there btw.

rare jungle
#

ok, so just do an if (hit.transform == null) return false;

rapid monolith
#

Yeah pretty much

sick ivy
#

Also some of your code would be a lot less confused if you used DrawRay

#

you should probably move castorigin a little away from the surface you hit so you don't immediately hit it again

#

Additionally, this will also detect Collider(s) at the start of the ray. In this case, the ray starts inside the Collider and doesn't intersect the Collider surface. This means that the collision normal cannot be calculated, in which case the returned collision normal is set to the inverse of the ray vector being tested. This can easily be detected because such results are always at a RaycastHit2D fraction of zero.

rare jungle
rapid monolith
#

You could add dir.normalized * someSmallNumber to the castOrigin

rare jungle
sick ivy
#

Instead of this nonsense
Debug.DrawLine(castOrigin, (Vector2)castOrigin + dir * 5);
just do
Debug.DrawRay(castOrigin, dir * 5);

rare jungle
#

updated code is above and I don't think it's working

sick ivy
#

hard to tell because you have a billion lines, but it looks like it's working to me

#

though you used the normal and not the dir, so it's offset from the face oddly

sage beacon
#

do Debug.DrawRay(castOrigin, hit.distance)

rare jungle
sage beacon
rare jungle
#

if you are talking about this:

dir = Vector2.Reflect(dir, hit.normal);

I followed a tutorial on bouncing bullet and just transfered that here

sage beacon
#

so its offset the same distance away from face regardless of direction

sick ivy
#

It depends on what you desire implementation-wise. If you need the same distance to solve the problem then you can also calculate that with projection

rare jungle
#

ok, so the code above works for the most part but there are cases like this:

#

here is how it should work

sage beacon
#

did u remove the offset code

rare jungle
sage beacon
#

add it back

rare jungle
#

I am not entirely sure what you mean by doing it with a normal

rapid monolith
#

Using the normal has it offset a bit but lower chance of going thru a corner I think

sage beacon
sick ivy
rare jungle
sage beacon
#

yea

#

wait

rare jungle
#

yeah it seems to be working now, couldn't find any issues

sage beacon
#

startPos = startPos + hit.normal.normalized * .01f

rapid monolith
#

dir works too

rare jungle
sick ivy
#

hit.normal is already normalized

sage beacon
#

fair point

rare jungle
#

why does it have a .normalized property then?

sick ivy
#

because it's a Vector3

sage beacon
#

cause its just a vector3

rare jungle
#

thanks a lot for the help with this!