1
votes

I'm calculating a vision cone for my enemy and, if the player is in it, I try to check if there is a collider between them with Physics2D.Raycast, so as to make sure the enemy does not see the player through walls.

The walls that can't be seen through are from a mesh, and have a mesh collider attached - see this pic. They are also on a specific layer, to which I'm trying to limit my Raycast.

My code is pretty straight forward. if the enemy is facing the player, and the player is close enough, fire the raycast.

if (distanceToPlayer < viewDistance && dirToPlayer == dir) {
    // player is in cone, check for collision
    LayerMask lm = LayerMask.NameToLayer ("WallCollisions");

    RaycastHit2D hit1 = Physics2D.Raycast(transform.position, (player.transform.position - transform.position).normalized, 1000, lm.value );
    RaycastHit2D hit2 = Physics2D.Linecast (transform.position, player.transform.position); 

    return true;
}

When breaking on the return statement to inspect the hit, it always looks like this:

{UnityEngine.RaycastHit2D}
centroid: {(0.0, 0.0)}
collider: (null)
distance: 0
fraction: 0
normal: {(0.0, 0.0)}
point: {(0.0, 0.0)}
rigidbody: (null)
transform: (null)
Non-public members:

Which means nothing was found.

Also, note that in the pic, the guard is surrounded by the mesh, meaning that regardless of direction, there should be a hit within the distance specified..

I have seen a lot of people do byteshifting to their LayerMasks, but mimicing theirs doesn't help, and frankly I don't understand how it would.

1
Your game is Top-Down or side-Scroller? I am asking because you always send the ray to the right side. You should use the normalized vector from the player to the enemy. Also, have you considered using Physics2D.Linecast? I think that in your case would be easier -> docs.unity3d.com/ScriptReference/Physics2D.Linecast.htmlCabrra
Hey, you're absolutely right about the direction. I was messing about with different directions when the direction to player didn't give me anything. I've edited the real direction back into the post. However, it doesn't make a difference with either direction, and the linecast doesn't find anything either..user2354664
Best way to debug this is using visual debugging. just before the RayCast or LineCast draw a Debug Line from the enemy to the player in that direction and see where it takes you. Let me know what happens.Cabrra
Drawing a ray became an extremely short line. Drawing a line gave me a line between the enemy and the character. Having noticed this, I removed most of the objects from my scene, and left only a square with a box collider. After positioning the player so that the debug-line went straight through the box collider, the hti from the linecast still returned the same as in the original post.user2354664
To draw a proper Ray you need to do "your direction*Your length". This is because the RayCast has length of infinity (by default). Try that and let me know.Cabrra

1 Answers

0
votes

This is now solved. To help out in the future, mesh collider is 3D, so a Physics2D raycast won't find it. Using a polygon collider solved everything.