Chasing detector

First of all, I want to apologize in advance if anything I write sounds strange or unclear. English is not my native language, so I’m doing my best to explain my issue clearly. Please bear with me, and feel free to ask for clarification if needed!

I’ve been working on a chasing detection system for my game, where I calculate the distance between the victim and the monster to determine if the monster is chasing the victim. When the monster is within a certain range, I trigger a specific sound effect and some visual cues to indicate chasing.

However, I’ve run into a problem: the system is too sensitive. Even when the monster isn’t actively attacking or intending to harm the victim, it’s still flagged as chasing. This makes the experience annoying and unrealistic for players, as the chasing effects (sound and visuals) keep triggering unnecessarily.

What I want to achieve is a more accurate detection system where chasing is only recognized when the monster is actively trying to attack or harm the victim, not just based on proximity alone.

Has anyone dealt with a similar issue? How can I improve the logic to differentiate between mere proximity and actual chasing behavior? Any suggestions or examples would be greatly appreciated!

Thanks in advance!

local monsterPosition = monster:GetPivot().Position
local victimPosition = victim:GetPivot().Position
if (monsterPosition - victimPosition).Magnitude < attackDistance then
  monster.State.Value = “Chasing” -- assume it is StringValue
else
  monster.State.Value = “Idle”
end

the most basic of it, but notice that this can be like you said “too sensitive” when the victim run faster than the monster, it can go in and out of the attack distance, thus making the monster go idle-chasing all the time.

you shall decide when the monster is in the chasing state, it may not rely solely on the distance check to go back to idle state too easily. one method is to have a chasing period such as 5 seconds, then do the distance check again to see if it should still chasing.

Need more info, maybe the code block that handles detection or a video?