AI - Fleeing question [SOLVED]

Using a FSM for AI and trying to come up with some decent code for an “Evading” script. I need the enemy who has entered this state to move from the player a minimum of 10.0 units away from the player.

Right now I am just picking a position 10 units in front of the player and moving there (not wise).

Any ideas/thoughts?

What I am currently doing:

        Vector3 directionToGo = GlobalManager.Player.transform.forward;

        Vector3 targetPosition = GlobalManager.Player.transform.position + directionToGo;

        //Look at target
        entity.transform.LookAt(targetPosition);

        //move to target
        MoveTowards(entity, targetPosition);

I guess from a mathematical point of view. If you drew a circle with the player in the center and a radius of 10 units, then you could draw a line from the player, through the enemy (would is trying to evade), which then intersects the circles circumference. Its this last intersect point I want the enemy to move to, make sense?

Just off the top of my head, something like this (untested)?

float desired_distance = 10.0f;
Vector3 escape_direction = monster.transform.position-player.transform.position;

Vector3 desired_position = player.transform.position+(escape_direction.normalized*desired_distance);

Works OK - but one thing is missing. First this is what I now have:

Vector3 escapeDirection = entity.gameObject.transform.position - GlobalManager.Player.gameObject.transform.position;

Vector3  targetPosition = GlobalManager.Player.gameObject.transform.position + (escapeDirection.normalized * desired_distance);

//Look at target
entity.transform.LookAt(targetPosition);

Vector3 forward = entity.transform.TransformDirection(Vector3.forward);

// Move the character
Vector3 direction = forward * entity.Stats.MonsterSpeed;
entity.EnemyController.SimpleMove(direction);

Monster speed in this case is 6.0f, however the enemy does move, but VERY slowly, more like 1/10th that, any idea why?

Try replacing SimpleMove with code that uses Move?

It could be you’re modifying the transform somewhere else in code; I’ve noticed that this breaks SimpleMove when it’s applying gravity.

Don’t think that’s it. I have chase code which works the same way and it works. Must be something in the code you supplied. The enemy does try and get away by taking the fastest direction, but he just walks VERY slow.

Forgot my state machine is being called once per second EXCEPT for movement states, of course I forgot to put this new Evade state in that exception list…