I´ve got my third person player character and would like him to move (with animation) to a location when a key is pressed. He should also avoid things in his way. My idea was to switch into AI-mode and use the AI-MoveTo node, but I have no idea of how to set this up.
Could anybody please help me with this or is there an other way of moving my character to a specified location?
Firstly, you could create an AI Controller and have it possess your pawn. Then you can execute AI Move To nodes to move the pawn (note: make sure you add a navigation volume added to your level to encompass the areas that navigation will need to be calculated). This makes it a little messy because you will effectively need to have a normal player controller and an AI player controller controlling the same pawn. Only one controller can “possess” the pawn, so you’ll need to have some funky setup in your player controller if you want to have other forms of movement for the player.
Alternatively, you can find the path yourself and write your own “Move To” method. You can use the “Find Path to Location Synchronously” node to find a path to a point. Get the nodes from the output and you now have an array of vectors that describes the path between two points. In order to use them you’ll need to loop through each point in the array (note: don’t use “for each” loops for this, you’ll need your own iterator and loop by dragging the execution node back to a branch to check the condition). Then for each point in the array, use “Add movement” to move your pawn towards the next point until the pawn is close enough to the point. I use something just like this currently so I can walk you through it if that’s the path you want to take.
Ok, I created an example from the third person template. I created an actor called “TargetPointActor” and placed it in the map behind some obstacles.
(EDIT: I almost forgot, you need to add a Nav Mesh Bounds Volume to your level and increase the size to encompass anywhere you want pathing to take place)
I then made an event in the character blueprint when you press “Enter”:
First it gets all actors of class “TargetPointActor” and grabs the first one (index 0). There will only be one so this will get the one placed in the level.
Then, it executes the “Find Path to Location Synchronously” from the character’s location to the TargetPointActor’s location. Dragging off the return value, you can get the “Path Points” that make up this path. Saving it to a variable makes it easy to handle later.
Then it runs a loop. It needs to loop until the index is beyond the length of the Path Points. First, it sets the iterator (I called it an “index” here) to 0 to ensure it starts at the beginning. Then it executes the main body of the loop (handled here by having an execution loop back around to a branch until the branch’s condition is not met).
The first part of the loop obtains the location of the path point at the current index and subtracts it from the player’s location. Then, “Vector Length” is used to get the distance between the two points. If the character is within 100 units (close enough) of the point, increment the index and restart the loop, so it starts pointing us towards the next point.
If the character is not within 100 units of the next point, the character needs to be moved towards the next point. By subtracting the location of the next point form the character’s current point (and yes, this is the same difference used in the previous part), we can feed that into the “World Direction” of a “Add Movement Input” node and it will move the character towards the second point.
A “Delay” is needed at the end here because without it, UE4 thinks there’s an infinite loop. Make sure the delay isn’t too large or you’ll see the character stuttering while it moves.
Anyway, that should do it. This could be easily modified to randomly get a point on the map and move to it. You can’t put this inside a function, unfortunately, because you can’t use “Delay” there. hope this helps!