How to enable the Seek Steering Behavior

The Seek steering behavior returns a Final Velocity that directs an agent towards a target position. The AI system takes into account the Current Velocity of the agent when computing the final velocity.

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

To use the Seek Behavior you must create an instance of the behavior as shown in line 1. Next, you must specify a target position for the agent to approach as shown in line 2. Assuming that you have created an instance of your character, you can then compute the Final Velocity as shown in line 3.

The getSteering() method takes as a parameter the instance of the character and a target position.

//Assume the dynamic action for the model was named kineticAction
//1. Create Seek Behavior
U4DEngine::U4DSeek *seekBehavior=new U4DEngine::U4DSeek();

//2. Set target position to seek
U4DEngine::U4DVector3n targetPosition=U4DEngine::U4DVector3n(2.0,0.0,3.0);

//3. Compute the final "seek" velocity for the character. uSoldier is an instance of your game character.
U4DEngine::U4DVector3n finalVelocity=seekBehavior->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);

}

The clip below shows the behavior of the Red agent approaching the Astronaut agent using the Seek Steering behavior.

Last updated