Fleeing and attacking

So, we will take from our old bit and add a small change, which will make the character run away from the Target instead of towards it. This will basically demonstrate the probability that you can give your AI reactions to various stimuli. Perform the following:

  1. We will get our State variable. Then, we want to subtract 1 from this, getting a 0 or 1 index. Next, we want to create a Select node and plug the results of the subtraction into the node.
  2. From there, we want Option 0 to be 1 and Option 1 to be -1. This will make the character either Run or Flee from Target. Now, zoom in to the Get Direction Vector node.
  3. Let's multiply Return Value by the Get Direction Vector node results. You must first convert Int to Float. Then, multiply this by the vector. Then, pull the vector back into Make Rot from X:
    Fleeing and attacking

Back to the action

Let's focus back on Enemy EventGraph at the bottom. We want to implement a way for our AI to wander. Perform the following steps:

  1. Start by searching for Get Controlled Pawn.
  2. Next, we want to perform Get Actor Location on the controlled pawn. Then the vector it returns will be pinned to a Vector + Vector node.
  3. From there, we want to create a Random Unit Vector node to generate a random vector from 0-1 on each individual axis.
  4. We must pull from the Stream variable again and create Random Float in Range from Stream. Set the Min value to -550 and Max to 550. Then, we want to multiply what it returns by Random Unit Vector.
  5. Lastly, the multiplied vector is added to the Vector + Vector node that we created in Step 2:
    Back to the action
  6. Now, we need to take the results of the Vector + Vector node and create a Move to Location node to instruct the possessed pawn to move to the Move to Location node of the vector.
  7. We will take a look back at our first state, which checks for Actors within a radius. After ForEachLoopWithBreak is completed, we want it to execute Move to Location. So, pull from that and first create DoOnce.
  8. We will do this because we don't want the bot to constantly be told to move to a location; we would rather have it do this after a random delay. So, now connect the Completed pin to Move to Location.
  9. From there, we want to assign a new event. This event will reset DoOnce after the random delay. So, after Event Begin Play, we want to create Assign MoveCompleted.
  10. From RecieveMoveCompeted_Event, create a Delay node. Pulling from the Duration node, we want to find Random Float in Range from Stream. We want to set the Min value to 2 and Max value to 5.
  11. After the Delay node is created, we want to connect this to the DoOnce node's Reset that we created in Step 7:
    Back to the action

The results!

We should now be able to set the chance that our AI will flee. This probability script was made less predictable with the random variant offset for the probability range. We accomplished this by creating a Finite State Machine, which transitions between the necessary states at any given moment. So the scenario is that we are approaching the wandering AI, and once we are in range, the AI will either attack or flee. We have given it a 25% chance of fleeing by default. The AI flees, and the timer to reset the bot's state starts. Seconds later, the AI returns to the wandering state.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset