There is a character call Alex who dont want to close to protagonist because of something. Therefore, it will run away when protagonist walk near it.
(How To Make The AI Run Away From The Player - Unreal Engine Tutorial)
When the EventBeginPlay, we need to add cast to the character that we want the AI run away from. Here, I cast the the character that player can control. Then we need to promote it to variable to store the information for later use.
Next, I need to add a custom event and named as "RunAway". Then, I need to make a code to find the rotation between the player(Get Actor Location with character REF) and the AI (Get Actor Location) so it knows which direction it should run away from the player.
The code after Find Look atRotation show how to find the correct rotation that AI should be running away from. First, I need to add Get Foward Vector which can let the AI run forward. But, in my case, I need Alex run away (backward) to me. Therefore, I need to get a vector multiply by float. The minus units that need to type means the units away from the player it's going to run towards (How far I want Alex run away to me). For the vector + vector, it can help to AI going in a straight line so it doesn't veer off track. Then, I need to break the vertor to make it stay on the same height (keep staying on the floor)
For the GetRandomReachablePointRadius, to set the radius that it can change a little direction (move left/ right) which make the walking more natural.
___________________________________________________________
After I followed the tutor, Alex can't walk, it just stay on the original place. This is because the tutor miss to tell us how to make the area that AI walk (Actually, he said he taugh how to make chasing by NPC in the opening). I do not need chasing by NPC, so I found another tutor which is let AI walk around in an area.
AI Random Roam | Basic Roaming - Unreal Engine 5 Tutorial
What I missed is the NavMeshBoundsVolume.
These functions are similar to what I explain above.
I created one more NPC called Sakura to walk around the slime village slowly.
It is friendly and always slime.
Comments