Simple AI That Moves Back And Forth

I am new to behavior trees and couldnt find a way to do this, what i want is the enemy will move left or right randomly and stop moving that way if there is no where to mov then rotate, go other way and do the same thing again.

Hey @30quickdead!

Check out this tutorial series from Ryan Laley, it covers exactly how to do that and a whole lot more if you’re interested in taking it further!

Disclaimer: This link is not associated with Unreal Engine, Epic Games, or their partners.
But Ryan Laley IS an authorized instructor. The disclaimer is referring to youtube. :slight_smile:
Hope this helps!

well not exactly, in the video it makes a patrol path i dont want to determine the way that my enemies go unless there is no other way. Cuz nearly all of my enemies will use this pattern so it will be hard for me to adjust all that points.

Can you describe a bit more about how you are wanting them to move? You could get a random point within a certain distance and then have them move like that?

  1. Create a new blackboard key and make it a vector type variable. Open up your AI Controller BP (create one if you don’t have it already), after Begin Play, get a Random Bool node, connect it’s return value to the Index pin of a Select node, right click on the node’s True / False pin, select float on the menu that pops up, then insert 1.0 to one of the pins and -1.0 to the other. Once that’s done, get the Get Blackboard node, drag out the Set Value as Vector node, then get a Make Literal Name node and copy & paste your blackboard key’s name into it, connect it to the Key Name pin of the node, then right click on the Vector Value pin of the node, hit the Split Struct Pin option on the menu that pops up, and plug the return value of our select node into the exposed x component (or might be y, depends on your orientation)

  2. Create a new task and in the behavior tree, trigger it after a composite of your choice (which to choose depends on your objective, for example if you want the AI character to either patrol or attack, you’d use a selector). Inside the task, constantly trigger an Add Movement Input node and assign the world direction to our vector type blackboard key.

  3. Create a new service and add it to your composite. Inside the service, constantly cast a line trace from the AI character to a little further. You can do that by plugging the controlled pawn’s location into the Start pin of a Line Trace by Channel node, then get the Get Actor Forward Vector (again, from the controlled pawn), multiply it with your desired value (which would be the margin between the AI character and obstacles), add the character’s location that we got earlier onto it, and plug the result into the End pin of our line trace node. Connect the Return Value pin of the line trace node into the Condition pin of a Branch node, and after it’s True pin, multiply our blackboard key with -1.

And that should be it! Hope this helps :innocent:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.