Boolean Blending for Aim States - How I did it

My solution was almost identical to Sam’s, except that I set the Aiming variable on Perception Change, instead of on Tick. There was a note about how casting on Tick isn’t a great idea, so I’m guessing it’ll be rewritten in a future lecture to be similar to what I did (or to cache the Character on Begin Play or something).

As an unrelated note, I’ve seen comments on how people don’t like the various mistakes that are in the videos (regardless of whether they were intentional mistakes), and I just wanted to weigh in on that. I think the mistakes are the single most important aspect of the course. Like, just a series of instructions of “do this, then that, then this other thing” is not helpful. What IS helpful is the reasoning behind why things are done, and also general tips for solving problems when they inevitably occur. Without those, we wouldn’t actually be in a very good position to make something after finishing the course. By knowing how to figure out what should be done next, and also how to work our way through issues, we’re able to actually put something together that works, which is ultimately the goal.

It seems like an awful lot of people seem to think that the goal is to make a Tank game, or a first person shooter. And I’m glad that the actual game is secondary to the important concepts of working with the engine to make things happen, and solve problems.

Anyway, that’s enough words. Here’s my AI Controller after adding to the end of the Perception Updated event. The duplicate nodes feel dirty, and if I didn’t already know we were going to re-do it, I’d probably make a function for it:

I did something similar but implemented in C++

AIController:

// SillikOne.


#include "GuardAIController.h"
#include "Perception/AIPerceptionComponent.h"
#include "Perception/AISense_Sight.h"
#include "Perception/AISenseConfig_Sight.h"
#include "Perception/AISense_Hearing.h"
#include "Perception/AISenseConfig_Hearing.h"
#include "Player/FirstPersonCharacter.h"


AGuardAIController::AGuardAIController() {
	// Creating Perception component
	PerceptionComponent = CreateDefaultSubobject<UAIPerceptionComponent>(TEXT("PerceptionComponent"));
	SightConfig = CreateDefaultSubobject<UAISenseConfig_Sight>(FName("SightConfig"));
	HearingConfig = CreateDefaultSubobject<UAISenseConfig_Hearing>(FName("HearingConfig"));

	// Configure the sight and hearing sense
	SightConfig->SightRadius = SightRange; 
	SightConfig->LoseSightRadius = SightLoseRange;
	HearingConfig->HearingRange = HearingRange;
	SightConfig->DetectionByAffiliation.bDetectEnemies = true;
	SightConfig->DetectionByAffiliation.bDetectNeutrals = true;
	SightConfig->DetectionByAffiliation.bDetectFriendlies = true;

	// Assign the sight and hearing sense to the perception component
	PerceptionComponent->ConfigureSense(*SightConfig);
	PerceptionComponent->ConfigureSense(*HearingConfig);
	PerceptionComponent->SetDominantSense(SightConfig->GetSenseImplementation());

	PerceptionComponent->OnTargetPerceptionUpdated.AddDynamic(
		this,
		&AGuardAIController::OnTargetPerceptionUpdate);

	// Assign this controller to team 1
	SetGenericTeamId(FGenericTeamId(1));
}

void AGuardAIController::OnTargetPerceptionUpdate(AActor* Actor, FAIStimulus Stimulus) {
	// If Player is sensed update CanSeePlayer with location
	if (Cast<AFirstPersonCharacter>(Actor)) {
		LastKnownPlayerPosition = Stimulus.StimulusLocation;
		bCanSeePlayer = Stimulus.WasSuccessfullySensed();
		}
	// flip flop Aim State
	OnAim.Broadcast();
}

The broadcast will fire each time Stimulus.WasSuccessfullySensed() is called

the ThirdPCharacter suscribed to the delegate and update bIsAiming:

[...]
void ATP_ThirdPersonCharacter::BeginPlay(){
	Super::BeginPlay();
	AController* AIController = GetController();
	if (!AIController) { return; }
	auto PawnController = Cast<AGuardAIController>(AIController);

	// Subscribe our local method to the controller update 
	PawnController->OnAim.AddUObject(this, &ATP_ThirdPersonCharacter::IsAiming);

}
 [...][
void ATP_ThirdPersonCharacter::IsAiming() {
	// Flip flop Aiming state
	bIsAiming = !bIsAiming; 
	UE_LOG(LogTemp, Warning, TEXT(" %s "), bIsAiming ? TEXT("Aiming") : TEXT("Not Aiming"))
}
 [...]	

And I made bIsFiring a UPROPERTY(BlueprintReadOnly) To grab the bool in anim BP.

I try to implement everything in C++ as we go following with almost the same architecture as Sam, so I only got the BehaviorTree and the Blackboard in BP.

Privacy & Terms