I thought about this approach, but there are a couple of problems with it. I am creating a MOBA map, so choosing the enemy "leader" is very problematic. Sometimes it is better to focus range carry, and sometimes it is better to focus melee. In terms of the approach, if an enemy hero chases the unit, then it will not work since the distance will be around the same.If the enemy heroes are always near each other then I suppose you could do something like this.
Choose one of these enemy heroes as the "leader" and periodically compare 2 things to your unit:
1: The angle between the leader and the unit to determine that the unit is facing away.
2: The unit's distance from the leader to determine that the unit is moving away and not closer.
#2 could be handled like this:
set variable Distance = distance between position of leader and position of unit
if (Distance < previousDistance) then set variable IsMovingAway = True
set variable previousDistance = Distance
Then you could add onto the If Then Else statement like:
if (#1 is true and Distance < previousDistance) then set variable IsMovingAway = True
I forget the math for the "facing away" stuff but there's probably an example of this idea in here: [Solved] - Angles (how to detect a backstab attack)?
I have an idea;What is a good way to detect that the unit is running away from the enemy heroes?
How do you think my opinionAlternate idea: When you issue a Move order towards a point, calculate the distance between that and each enemy hero. If it's not near any of them (distance <= 800) then you can mark the unit as "retreating".
Only issue is this requires manual orders so a unit that's naturally fleeing without being ordered to do so will ignore the trigger.
Anyway, it's very difficult to figure this out since there's nothing stopping a player from being sandwiched between multiple heroes. They could be fleeing from one while running towards another.