miwarnec / uMMORPG2D

0 stars 0 forks source link

2d remastered (didn't check other assets), monsters won't attack around corners. #5

Open iamallama opened 3 years ago

iamallama commented 3 years ago

Versions (please complete the following information)

Describe the bug Monsters won't attack a player when they come to a stop around a corner of the navmesh. If you get a monster to aggro and follow you, and you walk around a corner (based on navmesh), but stop near the edge of that corner. The monster will walk up to within attack distance for their skill (direct line of sight), but the call to Entity.CanAttack will return false because of a call to NavMesh2D.Raycast that hits the corner and so thinks there is an obstacle in the way. This can sometimes be true that there is an obstacle in the way, but with the navmesh agent radius buffers that get built into the navmesh, it's often unlikely to be the case. See the image below: Client

What you see here is the scene view. You can see the blue outlines of the navmesh wireframe. And the player moves around a corner of the navmesh (with apparently clean line of sight due to buffers). The monster moves up to their stop position for the attack and stops within the range of their attack distance, but won't attack due to the raycast in Entity.CanAttack going off mesh. The red lines that show up are a debugging that I added that calls Debug.DrawLine between the monster and player when the raycast goes off mesh. This shows for 5 seconds each line and fires once per frame.

To Reproduce Steps to reproduce the behavior, please explain every single click that is needed:

  1. Open Unity Editor.
  2. Perform steps the first two changes in the list above (makes it easier to reproduce).
  3. Run Editor and start game as Server & Play.
  4. Walk over to the monster to get them to follow you.
  5. Move around the corner of one of the bushes, but so that you are close to that corner.
  6. Monster walks over to the opposite side of corner and then immediately goes into idle state.

Expected behavior Monster should attack when in proximity and no obvious obstacle in it's line of sight to player.

Screenshots/Video See above.

System (please complete the following information):

Additional context Removing the raycast check in Entity.CanAttack allows attack to continue, but at the cost of allowing attacks through walls, etc. Since the raycast works using the navmesh which would include any agent radius buffers. Places with narrow paths would basically be near impossible to attack through because the narrow paths give very small sight lines. Perhaps the navmesh raycast should be replaced with a physics2d raycast which should help remove the agent radius buffer issue. Or some other method to determine stopping position that takes the navmesh into account would be needed, though harder.

This could maybe be done by calculating a path to the entities position and checking the last leg of the path generated. If greater than the attack distance, then just moving within proximity should be fine. If less than attack distance, move to the start of that last leg which should be within attack distance and also most likely the closest position with a "clear shot" on the navmesh to the target.

DanielTizon commented 3 years ago

I confirm it is happening too in ummorpg 2d remastered.

miwarnec commented 2 years ago

physics2d raycast might be the solution here

miwarnec commented 2 years ago

hm. the tree is technically an obstacle. will have to think about that one for a bit

miwarnec commented 2 years ago

the navmesh raycast technique in CanAttack is from ummorpg3d. I think that was important. will have to experiment a little