Following the agent

We want our Agent to follow its leader agent in whichever direction it goes. While still following the other logic to bounce off walls and the movement behavior we plan to introduce, we will create an example showing how this will affect the agent's movement as follows:

Following the agent

This step requires us to create a few variables and one function. We want to hold our Follower and Leader actors when operating in a free setup. We also need LeaderDirection to hold the direction that the agent should be moving in. Lastly, we can mark agents to be followers or leaders by enabling or disabling a Boolean called isLeader.

With these variables, we can keep track and prevent leaders from following followers, which can create interesting movement behavior but not what we intend to make happen in this demonstration.

Follow or lead

Let's open our FlockingBall BP class and view EventGraph. Find where we will update our RelativeRotation pin for our Spring Arm variable. Between this and Calculate Direction, we will add this new set of instructions:

  1. First, we need to reference any actor near our agent. We can do this by the SphereOverlapActors node. Right-click on the graph and search for this.
  2. Now, let's get the Get Actor Location node. Right-click, find this, and plug it into Sphere Pos of the node we created in the previous step. Next, set the Sphere Radius value to 300. Now, let's pull from Object Types and create the Make Array node. So, we should populate Object Types with Physics Body. Next, we want to set our Actor Class Filter node to Flocking Ball to prevent unwanted results. Lastly, create another Make Array node, right-click on the graph, and search for it. Then, populate the first element with a reference to Self. Then, plug this array into actors to ignore. Comment this area Search for nearby FlockingBalls.
  3. Now, let's focus on the Out Actors array. Right–click on the graph and search for For Each Loop. Let's pull from Array Element and cast this to FlockingBall.

    We have to know whether the agent we attempt to follow is following us. Otherwise, we can experience unwanted movement behavior.

  4. From the Casting variable, let's find the Leader variable and compare it to Self with equal (==). Take the Condition pin and create a Branch node. If it is False, we need to check now whether we are Leader, and if we have either Leader or Follower.

    Before we move on, comment this branch with Don't follow, followers as leader:

    Follow or lead
  5. So, search for the isLeader variable and create this variable. Next, search for Leader and check whether it is valid by creating the Is Valid node (which is a function). We have to do the same thing for another variable called Follower.
  6. Now, connect all three Boolean conditions into an OR node. This will return True if any of our conditions return True. If any of these conditions return True, they're already following an agent or leading another agent:
    Follow or lead
  7. From this OR node, let's search for a new Branch node and connect it to the Condition pin. Now, if this Branch node value is False, let's create a node called IsValid (macro). The object we want to check is the casting we made in step 3. Pull this and create a reroute node near the IsValid (macro) node so that it can be used by other nodes easily.
  8. If IsValid (macro) returns Is Valid, we want to create a Branch node. We want to check two conditions to be True with an AND logic gate in this statement.
  9. First, let's get the Leader variable from the reroute. Then, we will compare it to Self with not equal (!=). Create an AND logic gate and connect the first pin to the results of the not equal (!=) condition.
  10. Next, let's grab the Follower variable from the reroute. Then, compare this node using equal (==). Next, connect this to the AND logic gate in the previous step.
  11. Lastly, connect this AND logic gate to the Branch node. If this node returns True, then let's set the Follower pin from the reroute. Plug in a reference to Self into the Follower pin. Then, set the local Leader variable from reroute.

    Comment this area If we don't have a leader, and we are not one. Find one:

    Follow or lead
  12. Now, go to the Completed pin of the For Each Loop node. Let's create a local Leader variable and check whether it is valid using Is Valid (which is a function). Now, create a Branch node and plug in the Condition pin from the prior node.
  13. If the Branch node returns False, we want to set the Leader Direction value of the SET variable to 0.0, 0.0, and 0.0. If our Branch node returns True, we want to calculate the direction toward our Leader variable.
  14. Create a new node called GetActorLocation, and from the return, we will create Get Direction Vector. Now, let's right–click on the graph and search for the local Leader variable. Then, create the GetActorLocation node from the Leader variable and plug this into the To pin of Get Direction Vector.
  15. From the Get Direction Vector node, pull Return Value and create Make Rot from Y. Next, we want to create Get Up Vector from the previous rotation. Then, from Return Value, this will go into Leader Direction of the SET variable.
  16. Both Leader Direction local variables will lead to area commented as Calculating Direction. We must also go to Calculate Direction and add Leader Direction to the final direction calculation.

    Comment this area Calculate Leader Direction:

    Follow or lead

    Note

    Now, if we hit Play, we can see what happens. Little groups that begin to form as followers find potential leaders by passing each other.

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

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