So seeing as there aren’t any tutorials for this yet (I know there are some upcoming that will be more in depth) I thought I would share a bit of how to setup the AI Perception Component for anyone who wants to get a head start and start messing around with it. You can also find my blueprint version of this here: https://forums.unrealengine.com/showthread.php?66810-Quick-AI-Perception-Jumpstart
(This is done with version 4.7.5)
These are the first and probably most important things you need to do. First create the perception component. Next you want to create a config for any senses you may want to use and add them to the component with ConfigureSense() Next you can set the sense that will override other senses. Finally you bind a function to be called when the Perception System gets a stimuli (ie. sees or hears something).
// Setup the perception component
PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("AIPerception Component"));
sightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(TEXT("Sight Config"));
PerceptionComponent->ConfigureSense(*sightConfig);
PerceptionComponent->SetDominantSense(sightConfig->GetSenseImplementation());
PerceptionComponent->OnPerceptionUpdated.AddDynamic(this, &AAIControllerUnit::SenseStuff);
I put this in the OnPosses function so that I know i’m dealing with a pawn or character of some kind but its not necessary (I needed it because the attack range was stored inside of my unit) Here we do some more setup for the sight sense and also make sure we can check for everything.
sightConfig->SightRadius = possessedUnit->sightRange;
sightConfig->LoseSightRadius = (possessedUnit->sightRange + 20.0f);
sightConfig->PeripheralVisionAngleDegrees = 360.0f;
sightConfig->DetectionByAffiliation.bDetectEnemies = true;
sightConfig->DetectionByAffiliation.bDetectNeutrals = true;
sightConfig->DetectionByAffiliation.bDetectFriendlies = true;
PerceptionComponent->ConfigureSense(*sightConfig);
Do some stuff in the function that we registered earlier. testActors is an array of actors that have been detected by the Perception System (see below on how to add actors that can be found)
void AAIControllerUnit::SenseStuff(TArray<AActor*> testActors)
{
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "I see you!");
}
Finally add this line to any actors that you want to detect where sightConfig->GetSenseImplementation() is the sense you are registering for. (ie. if you want hearing you need to add the appropriate sense) In mine I put this in the controller of one of my AI though you could put it directly in the actor itself if you wanted.
UAIPerceptionSystem::RegisterPerceptionStimuliSource(this, sightConfig->GetSenseImplementation(), GetControlledPawn());
And that’s it, your done! If you AI can see anything registered it should fire the event and you can do stuff accordingly. Let me know if this was helpful or if you have any questions!
Edit: So idk if this will affect everyone but if your having a lot of crashes check this post out: https://answers.unrealengine.com/que...rce-issue.html
Thanks,
-Chris