Have an AI move away from the player

Hello, I’m using Unreal for a university project (have never used it before either) and I’m trying to do the following: using the default template for a third person game I want to essentially have an AI move away from the player if the player is within a certain radius of the AI. I’ve currently attempted this tutorial (How To Make The AI Run Away From The Player - Unreal Engine Tutorial - YouTube) however it doesn’t do what I want and I believe it is because the player character is instantiated on play rather than existing on play. What would be the best way to try and impliment what I want my AI to do?

Hey there @BeamBon! Welcome to the community! Are you receiving an error that the character reference isn’t valid? Since the player spawns a tick before begin play when you look for it, it should always be accessible.

If that’s not the case, is it not moving at all or is it moving oddly? If moving oddly you may have multiplied the forward vector by another vector which is a common issue I see with this tutorial. To fix that you’d only have to right click the bottom node in multiply and convert it to a float. May I see the BP? I can likely help point out the issue directly.

image
Hello, sorry for not responding very quickly. I’m currently using this blueprint to do one other AI behaviour, which is to have this AI entity move to a random place every few seconds, so have been trying to encorporate this other AI behaviour with it. It could be that is causing the issue?

If you are able to help, I really appreciate it!

Oh dear, yes that is a part of it. First thing I noticed was a delay on tick. If at all possible, never put a delay that fires every tick. So Delay nodes are Asynchronous (denoted by that little clock on their node). This means that every single time you tick, you’re creating a new delay running on it’s own time (if your fps is 60, you’re doing this random delay 60 times a second) so let’s say this runs for only a second, you have 60 commands queued up that can happen at random intervals, overlap, change, etc. This will nullify basically anything you try to do with AI movement and also cause incredible overhead.

I’d recommend rewriting that person entirely. I usually do recommend using blackboard for AI stuff if it get’s more in depth than a couple of modes. To make the delay work out here, it’d be ok(not great) to add a Branch node into a Do Once node at the beginning after Event Tick. Then for the branch you make a bool for if it’s running from the player or not and set that at the beginning and end of your run away paths. Then make the Completed or Fail start on the AI move to to reset the original DoOnce.

This isn’t the best setup and will need more tweaks but it’s a way to slightly modify your current setup if all you need is the basic of basics.

I’ve got a tutorial that goes over random walk and run away while using Blackboard for the AI if you want to give that a shot.

Disclaimer: One or more of these links are unaffiliated with Epic Games. Epic Games is not liable for anything that may occur outside of this Unreal Engine domain. Please exercise your best judgment when following links outside of the forums.

Thanks, I’ll try and look at this when I get back from university. I’ll let you know how well it goes

Hello, I’m a bit confused on what blackboard is, but I have followed the tutorial you posted to try and get random movement and running away working. I’ve removed the event tick and am now just trying to get the AI to move away from the player (rather than running away if there is a noise like the tutorial) but I’m still getting stuck on getting a reference to the player itself and I think it’s because when I press start in the scene I don’t know if the player character component exists or not when I’m trying to get it.

I apologise if I’m being annoying with these questions. I do really appreciate the help!

Oh sorry about that! I thought Matt went over blackboard in this tutorial, it seems he just does it outright. (Which is fine too! Simple AI doesn’t need a wild state machine setup).

So generally if you want it to just run from the player, you’ll want to make the noise on the player character’s BP. The report noise node is what you’ll use to make the sound for the AI itself. So generally you don’t even need to reference the player directly like in this video of the tutorial:

I can probably show you where to go if you can show off the blueprints you have set up! We’re here to help, questions aren’t annoying, they are how we learn!

image
Here are my current blueprints, which are quite similiar but have two differences, I’m not using the event tick node now and I have two different versions of run away based on the two tutorials I have followed (currently using the method from the original tutorial I used) I should mention that I’m not planning to use noise, I just want the AI to run if it notices the player within a certain distance. I’m not sure if there is a way to do this however. Sorry if I didn’t make that clear before.

image
There is a second part btw, I just can only do one screenshot at a time since I’m new!

No worries! So you just want it to have vision and not hearing for the player? If so, you’d still use AI perception, just the vision portion. It’s done basically the same way. If you’d rather it be whenever the player comes within a specific range regardless of if they are quiet or in vision, you could just keep an overlap sphere for that.

Your first post is so far zoomed out it’s tough to read without inference, but it looks mostly sound. To check and see if your player reference is failing, you can take your Fail from your Beginplay assignment of the cast reference go to a print instead of progressing. Just have it print that the cast had failed or similar. That should tell us if it’s cast is failing so the var itself is set to null.

Thanks! I was wondering how to debug with graphs so that helps a lot and I’ll give the AI perception a try tomorrow morning.

Yep! You can even use proper breakpoints like you would in code, but they function a bit differently. Instead of prints you can also just inspect the variables and do breakpoints, but I figured the easiest way was to show you how to print out what’s going on in the meantime. Here’s some docs on some of the debugging tools and breakpoints:

Hello, just letting you know that I think it is working now, after using the sphere overlap node. I think I need to fine tune it slightly and I’m pretty certain it is working.

Thanks again for all the help! I’m going to start attempting so of the other parts of my assignment now, but I’ve now got part of the checklist sorted!

No problem! When you do need more help, a fresh thread would be the way to go. Good luck and happy developing!