How to set the Flee Steering Behavior

The Flee Steering Behavior has the opposite behavior as the Seek Steering Behavior. That is, instead of approaching the Target Position, it moves away from the Target Position.

The code snippet below shows how to compute the Flee Steering velocity.

Line 1 shows the creation of the Flee instance object. The Flee behavior requires a target position. This position is provided in line 2. Assuming you have created an instance of your character, you can then compute the Final Velocity as shown in line 3.

//1. Create a Flee Steering Behavior
U4DEngine::U4DFlee *fleeBehavior=new U4DEngine::U4DFlee();

//2. Set the Target Position to flee from
U4DEngine::U4DVector3n targetPosition=U4DEngine::U4DVector3n(2.0,0.0,3.0);

//3. Compute the final "flee" velocity for the character. uSoldier is an instance of your game character.
U4DEngine::U4DVector3n finalVelocity=fleeBehavior->getSteering(uSoldier->kineticAction, targetPosition);

//Set the final velocity y-component to zero.
finalVelocity.y=0.0;

//Check if the resulting velocity is set to zero. Do this as a a safeguard. I have to fix this issue.
if(!(finalVelocity==U4DEngine::U4DVector3n(0.0,0.0,0.0))){

    uSoldier->applyVelocity(finalVelocity, dt);
    uSoldier->setViewDirection(finalVelocity);

}

Last updated