ToonTanks -> Tank refuses to die

Hi, I’m using Unreal 5.1, following the Toon Tanks course.
At the end of the “Handling Pawn Death” video, everything works as expected except the Tank, which absolutely refuses to die. It gets hit (it logs out messages when hit and damaged) but it never dies.
The turrets work fine. They do disappear as they should/when they die.
Here goes the code:
(Thanks in advance!)

// Fill out your copyright notice in the Description page of Project Settings.


#include "Tank.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Kismet/GameplayStatics.h"

ATank::ATank()
{
	SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
	SpringArm->SetupAttachment(RootComponent);

	Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
	Camera->SetupAttachment(SpringArm);
}

void ATank::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    PlayerInputComponent->BindAxis(TEXT("MoveForward"), this, &ATank::Move);
    PlayerInputComponent->BindAxis(TEXT("Turn"), this, &ATank::Turn);

    PlayerInputComponent->BindAction(TEXT("Fire"), IE_Pressed, this, &ATank::Fire);
}

void ATank::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    if (TankPlayerController)
    {
        FHitResult HitResult;
        TankPlayerController->GetHitResultUnderCursor(
        ECollisionChannel::ECC_Visibility,
        false,
        HitResult
        );

        RotateTurret(HitResult.ImpactPoint);
    }
}

void ATank::HandleDestruction()
{
    Super::HandleDestruction();
    SetActorHiddenInGame(true);
    SetActorTickEnabled(false);
}

void ATank::BeginPlay()
{
	Super::BeginPlay();

    TankPlayerController = Cast<APlayerController>(GetController());
}

void ATank::Move(float Value)
{
    FVector DeltaLocation = FVector::ZeroVector;
    // X = Value * DeltaTime * Speed
    DeltaLocation.X = Value * Speed * UGameplayStatics::GetWorldDeltaSeconds(this);
    AddActorLocalOffset(DeltaLocation, true);
}

void ATank::Turn(float Value)
{
    FRotator DeltaRotation = FRotator::ZeroRotator;
    DeltaRotation.Yaw = Value * TurnRate * UGameplayStatics::GetWorldDeltaSeconds(this);
    AddActorLocalRotation(DeltaRotation, true);
}

And have you added logs in relevant places to see where it’s going wrong?

(Also the health component and game mode are more relevant)

Thanks for your answer. I added logs in both the health component and game mode.
I get both logs from the turrets -obviously- when they get hit and when they die.

I don’t get any log from the health component regarding the tank… I have double-checked the code and I cannot find anything wrong there. I also checked the collision settings in the tank and it’s the same as for the turret and projectile.

Here is the code of both the Health Component and the Game Mode. Thanks again!

// Fill out your copyright notice in the Description page of Project Settings.


#include "HealthComponent.h"
#include "GameFramework/Actor.h"
#include "Kismet/GameplayStatics.h"
#include "ToonTanksGameMode.h"

// Sets default values for this component's properties
UHealthComponent::UHealthComponent()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;
}


// Called when the game starts
void UHealthComponent::BeginPlay()
{
	Super::BeginPlay();

	Health = MaxHealth;

	GetOwner()->OnTakeAnyDamage.AddDynamic(this, &UHealthComponent::DamageTaken);
	ToonTanksGameMode = Cast<AToonTanksGameMode>(UGameplayStatics::GetGameMode(this));
}


// Called every frame
void UHealthComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}

void UHealthComponent::DamageTaken(AActor* DamagedActor, float Damage, const UDamageType* DamageType, class AController* Instigator, AActor* DamageCauser)
{
	UE_LOG(LogTemp, Error, TEXT("Greetings from Health Component"));

	if (Damage <= 0.f) return;

	Health -= Damage;

	if (Health <= 0.f && ToonTanksGameMode)
	{
		ToonTanksGameMode->ActorDied(DamagedActor);
	}
}

// Fill out your copyright notice in the Description page of Project Settings.

#include “ToonTanksGameMode.h”

#include “Kismet/GameplayStatics.h”

#include “Tank.h”

#include “Tower.h”

#include “ToonTanksPlayerController.h”

void AToonTanksGameMode::ActorDied(AActor* DeadActor)

{

if (DeadActor == Tank)

{

    UE_LOG(LogTemp, Error, TEXT("GameMode works fine"));

    Tank->HandleDestruction();

    if (ToonTanksPlayerController)

    {

        ToonTanksPlayerController->SetPlayerEnabledState(false);

    }

}

else if (ATower* DestroyedTower = Cast<ATower>(DeadActor))

{

    UE_LOG(LogTemp, Error, TEXT("GameMode works fine"));

    DestroyedTower->HandleDestruction();

}

}

void AToonTanksGameMode::BeginPlay()

{

Super::BeginPlay();

HandleGameStart();    

}

void AToonTanksGameMode::HandleGameStart()

{

Tank = Cast<ATank>(UGameplayStatics::GetPlayerPawn(this, 0));

ToonTanksPlayerController = Cast<AToonTanksPlayerController>(UGameplayStatics::GetPlayerController(this, 0));

StartGame();



if(ToonTanksPlayerController)

{

    ToonTanksPlayerController->SetPlayerEnabledState(false);

    FTimerHandle PlayerEnableTimerHandle;

    FTimerDelegate PlayerEnableTimerDelegate = FTimerDelegate::CreateUObject(

        ToonTanksPlayerController,

        &AToonTanksPlayerController::SetPlayerEnabledState,

        true

    );

    GetWorldTimerManager().SetTimer(

        PlayerEnableTimerHandle,

        PlayerEnableTimerDelegate,

        StartDelay,

        false

    );

}

}

Does the tank have a health component?

Ohh that! …it does now… for fox sake --Insert facepalm here–

Thanks a lot!!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms