How to create 'fleeing' AI? (Script to send gameObjects running around an environment)

I want to have some of my gameObjects run around a 3D environment as if fleeing from something - constant velocity, 'random' direction and will turn and continue running on colliding with obstacles.

I'm totally new to coding (working in JS at the moment), so does anyone have some similar script, or know of a relevant example to work from? Apparently SeekSteer or UnitySteer might be useful, but I'm worried about coding anything too complex/CPU-intensive for a large number of these gameObjects onscreen at once.

Thanks =)

How you implement this entirely depends on how your scene is set up. Whether it's a tile-based world, a free roaming flat world, a maze-like environment, or a multi-level environment with stairs and different levels, or something else - these factors will all effect which method is best to use.

That said, your options can probably be narrowed into one of these two broad categories:

1) using pathfinding with a pre-computed network of nodes which describe routes around your environment, to find a route away from the object being avoided.

2) using 'virtual sensors', such as raycasting to detect obstacles and clear routes in the proximity of the fleeing character, while otherwise 'blindly' fleeing.

Option 1 will probably take more setting up before you get results, but is more likely to give you more 'intelligent-looking' behaviour. Option 2 could be set up more quickly - for example if your world is relatively flat with easily detectable walls, a single ray cast in the object's forward direction would be enough for detection, and having it turn away from the wall if the raycast result distance is too close.

Here are two examples of pathfinding in Unity:

http://www.arongranberg.com/unity/a-pathfinding/

http://angryant.com/path

Well, one simple approach to agent behaviour would be to check against the "chasers" position and take the direction between the chaser and the fleeing agent.

This is C# but JS should be very similar.

Vector3 direction = transform.position - chaser.transform.position;

Then, you could normalize the direction and perhaps add some noise to the normal to get a "not-always-perfect-direction". If you sample this like every second or so, you'll get a little bit more realistic motion instead of doing this per frame.

Then, every frame just move along the direction (normal * speed) to apply motion.

If you are looking for more complex behaviour including obstacles, you might want to learn pathfinding and select a "safe" location for the fleeing agent and start to walk that direction.