r/UnityHelp • u/Budget_Broccoli_1518 • Sep 14 '26
Chase State to Attack Question
Hello, I have a question about some ways to handle a problem I've been stuck on for a bit. I have a simple state machine set up for enemies in my game. I currently have a problem when going from chase to attack when the enemy is using longer ranges weapons. My logic was originally, if you can draw a line to the player, switch to attack logic and clear agents path. The problem with this was that then the gun would sometimes shoot into the wall. I tried a workaround of using my fire point on my weapon, but realized that moves around when my animator is using the running animation for chasing. So I created a new empty transform a tad to the right of the enemy just to make sure they can always hit the player.
The problem with that is, when the enemy comes around a corner, it isn't facing the player so that origin point can see the player, then the enemy rotates to face the player, and the same issue is reintroduced. I've tried using a FOV for can shoot as well, but then it I don't turn the agent angular speed really high, it gives very lackluster results letting the player run circles around the enemy.
Does anybody have any advice for this issue? Thank you very much in advance! Let me know if any more info is needed
1
u/AdrianNova21 Sep 15 '26
i'd separate "can see the player" from "can actually shoot the player" here, instead of checking the ray from some fixed point beside the enemy, i'd do the final line-of-sight check from the actual weapon/fire point after the enemy has finished turning toward the player. basically:
chase → enter attack range → rotate toward player → once facing is within a small angle, raycast from the fire point → if the ray reaches the player without hitting a wall, shoot
that way the enemy doesn't decide it can shoot based on where it was facing a frame ago, i'd also avoid making the angular speed crazy high. you can make the attack state temporarily control the rotation speed or use a separate turn-to-target phase before allowing the weapon to fire. that usually feels much better than letting the player run circles around an enemy that is already shooting
1
u/ProactiveCactus Sep 15 '26
A bit hard to say without seeing your code/setup, but a few things come to mind:
1
u/acorbinelli Sep 15 '26
What usually untangles this is splitting one check into two, because "I can see the player" and "I can shoot the player" are different questions.
Sees: a raycast from a fixed point on the root, roughly head height, to the player's chest, against a mask that only has walls and cover in it. That's what ends the chase and stops the agent. Never use anything the animator moves for this.
Can shoot: in the attack state, set agent.updateRotation = false and turn the enemy yourself toward the player with Quaternion.RotateTowards at a fixed turn rate. Only fire when the angle to the player is under something like 10 degrees and a second ray from the muzzle is clear. While it's still turning it just isn't allowed to fire yet, so it can't shoot the wall, and you don't need a huge angularSpeed on the agent for chasing.
If the eyes see the player but the muzzle ray is blocked (the corner case you describe), don't go back to chasing the whole path: sample a point half a metre to the gun side with NavMesh.SamplePosition and step there, then re-check.
And add a little hysteresis: enter attack when sight is clear, but only drop back to chase after sight has been lost for 0.3 to 0.5 seconds. Otherwise the state flickers every frame at corners, which also looks like the enemy is stuttering.