Line trace by channel = death

hey i have a line trace by channel for the first person template gun and have got it to hit my ai however i need to have the ai die but i don’t know how. I have gotten the ai to blow up when inear to the character but i have no clue how to do this.

Hi there, hope you’re doing well :slight_smile:
I don’t know if I got the problem 100%, you want to kill the AI actor when the line trace hits it? Are u making this in blueprints or c++?
If I got the problem right, you can do it by having a bool variable like “bIsDead” in your AI class that will be checked on tick function. In your MainCharacter class you make the line trace and checks the actor that was hit. If it’s the AI actor, cast to the AI and then set the bool bIsDead to true.
In blueprints will be something similar to this:
BP_AI

BP_Character

And in c++, would it be something like this:
AAIActor.h

public:
	bool bIsDead;
        FORCEINLINE void SetIsDead(bool bDead) { bIsDead = bDead; };

AAIActor.cpp

void AAIActor::BeginPlay()
{
    Super::BeginPlay();
    bIsDead = false;
}
void AAIActor::Tick()
{
    if(bIsDead== true)
	{
		// Call the dead function that you created
	}
}

MainCharacter.h

        UPROPERTY(EditAnywhere, Category = "Collision")
	TEnumAsByte<ECollisionChannel> TraceChannelProperty = ECC_Pawn;

	UPROPERTY(EditAnywhere, Category = "PickUp")
	float TraceLenght = 200.f;

MainCharacter.cpp

       // This code will be inside the function that you want to make the line trace
	FHitResult Hit;
	FVector TraceStart = FirstPersonCameraComponent->GetComponentLocation();
	FVector TraceEnd = FirstPersonCameraComponent->GetComponentLocation() + FirstPersonCameraComponent->GetForwardVector() * TraceLenght;

	FCollisionQueryParams QueryParams;
	QueryParams.AddIgnoredActor(this);

	// LineTrace
	GetWorld()->LineTraceSingleByChannel(Hit, TraceStart, TraceEnd, TraceChannelProperty, QueryParams);

//checking if the hit actor is the AI
    if(Hit.GetActor()->IsA<AAIActor>())
    {
        AAIActor* HitActor = Hit.GetActor();
        HitActor->SetIsDead(true);
    }

if I get it wrong or you don’t understand somethin, please let me know!

1 Like