#Raycasts
1 messages · Page 1 of 1 (latest)
Tip: post the updated code where you have fixed these things we pointed out
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;
}
green lines represent normals and white ones represent the raycast
so now it works at certain angles but not at others 0_o
Instead of all those hit.transform/hit.collider null checks you could just break out of the loop if nothing is hit
hit.collider is always there if hit.transform is there btw.
ok, so just do an if (hit.transform == null) return false;
Yeah pretty much
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.
is there an easy way to do that or do I need to calculate the distance between the enemy and a hit.point and then get a sign of that am multiply the move amount?
You could add dir.normalized * someSmallNumber to the castOrigin
not entirely sure how I'd use that
Or the normal
Instead of this nonsense
Debug.DrawLine(castOrigin, (Vector2)castOrigin + dir * 5);
just do
Debug.DrawRay(castOrigin, dir * 5);
updated code is above and I don't think it's working
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
do Debug.DrawRay(castOrigin, hit.distance)
a few lines went through objects, I would call that working, other than that yeah, angles seem fine
where did I do that
should be offsetting using the normal tho
if you are talking about this:
dir = Vector2.Reflect(dir, hit.normal);
I followed a tutorial on bouncing bullet and just transfered that here
so its offset the same distance away from face regardless of direction
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
ok, so the code above works for the most part but there are cases like this:
here is how it should work
did u remove the offset code
yeah
add it back
I am not entirely sure what you mean by doing it with a normal
Using the normal has it offset a bit but lower chance of going thru a corner I think
offsetting the start position in the direction of the normal
You cannot not offset it, else this will happen
yes but how, is this a proper way to do it?
startPos + dir.normalized * .01f
yeah it seems to be working now, couldn't find any issues
startPos = startPos + hit.normal.normalized * .01f
dir works too
yeah I did it up in the casting part but ig this is more optimal
hit.normal is already normalized
fair point
why does it have a .normalized property then?
because it's a Vector3
cause its just a vector3
thanks a lot for the help with this!