Using C++ to create an AI Patrol Enemy

Hey everyone,

I am creating a Patrol AI for a class assignment within Unreal Engine (duh) using the FirstPerson template but I am not getting the result I want. My idea was to create a RayCast that waits for the player and when the player goes into the RayCast the enemy will begin following the player. The assignment calls for us to use only C++. Here is the .cpp work I have and what I am trying to get:


void AMyAICharacter::RayCast()
{
	FHitResult* HitResult = new FHitResult();
	FVector StartTrace; //I want the Ray to start at the enemy 
	FVector EndTrace; //(StartTrace * someNumber) + StartTrace

	//I want to put here if(HitResult hits the player character) then move toward the player
}

I am really lost here. All I want is a very basic patrol AI to at least have something to submit as the assignment is due tonight. As a side note, is it possible to put the third person character and its accompanying animations into a first person game?

You are missing the trace type. You just got the start and end trace.
You may also want to add a visual debuger to see the trace system while you are testing it.

You also have to implement forward vectors, to get the vectors between player - patrol, on the x axis and rotation to position vector, and then add some motion system to move towards you. It’s not that easy to build.

First fix the trace system so it’s complete. Once you get it running just play with it, make different traces, just stay in the trace system a while and toy with it. Don’t rush many things at once, once you know all the things it’s easy for you to build anything.

This is for a school assignment which I have received no guidance on. I’ve tried looking for C++ specific tutorials online but I have only found Blueprints and I’m struggling to transfer that over. What exactly is a trace type? I want to be sure I’m fully understanding what you are saying. I looked it up and what I got was you go into project settings to do them. Does that sound about correct?

Search for
UKismetSystemLibrary::SphereTraceSingle
This is type of trace system, it can have many types not just spherical or single line trace.

ETraceTypeQuery::
This is trace list types. It contains your trace variable, your visual debuger variable + other things like mesh types, actors to ignore and so on.

You also have to write the debuger separate but you mention it in the ETraceTypeQuery list. You can write it more than one way.

So you need all these things for your trace system, you have to learn and search for these things. Learn on your own, I don’t give code that easy.

Once you put the trace system toghether you should just remain in it and play around with it with different type of traces to understand the trace system, modify values and types, then after that you will get into transforming actor locations to what ever you need to move the actor based on your trace system when the trace system go’s off.

I posted this before so I might as post it again here.

Body of  some ifs ...  {
FHitResult CaTraceHit;
bool bRightFootTraceHit = UKismetSystemLibrary::SphereTraceSingle(this,


     YourObjectLocation + FVector(0.f, 0.f, 50.f), /*Trace start*/
     YourObjectLocation + FVector(0.f, 0.f, -100.f),  /*Trace end*/ 
     10, /*Trace radius*/ 
     ETraceTypeQuery::TraceTypeQuery1, false,  /*Visibility chanel */
     ActorsToIgnore, EDrawDebugTrace::ForOneFrame, CaTraceHit, true) }

You can modify this according to your needs, like make it single line trace maybe change chanels and so on.

Hmm how is C++ support from EPIC.

I would say “Epic Disaster”
You searched right, ha ha ha. Good luck searching because you are not going to find it, because the forum search filter is awful and this forum is it where you can get some information, the C++ doc page is very mecanical and as a beginer you are not going to understand it.

That’s the issue. The way I’ve been being taught is half in blueprint, suddenly stop, then the other half in c++. I haven’t fully been taught how to use either. By “fully” I mean I haven’t mastered either enough to be comfortable using it. I’m sorry if it seemed like I was asking for specific code. I’m asking questions at my highest understanding of the material given. I’ve begged every teacher I’ve had to help me and I’ve never gotten much. I rely heavily on forums like this to teach me something I don’t understand.

You can use the trace system with the pet tutorial.

Combine them and then improve them I guess, so he just sits there and when the trace is activated put the follow pet into action, meaning his trace hits you, you can make him walk around first I guess. I made an actor follow me on mountains, when he is in some range you ca also make him attack you, tho I did not use it for that.

You can use
YourActor location with GetActorForwardVector() times speed.
But there are lines of code you have to figure out.

I think after you solve your trace system this is for you.

You have enough information here to make things work for you.