LineTrace hits Pawn, but can't cast to character

I want to get the LineTrace hit result and cast to my ABaseCharacter (actor derived from ACharacter). The LineTrace hit result GetActor() returns a BP_BaseCharacter but cannot cast it to ABaseCharacter*

.cpp


ABaseCharacter* USkillComp::GetCharacterFromLineTrace()
{
    FHitResult OutHit;

    FVector ForwardVector = GetOwner()->GetActorForwardVector();

    FVector Start = GetOwner()->GetActorLocation() + FVector(0.f, 0.f, 64.f);

    FVector End = ((ForwardVector * RefuelReach) + Start);

    FCollisionQueryParams TraceParams(FName(TEXT("")), false, GetOwner());

    DrawDebugLine(GetWorld(), Start, End, FColor::Green, false, 0.5, 0, 1);

    if (GetWorld()->LineTraceSingleByObjectType(OutHit, Start, End, FCollisionObjectQueryParams(ECollisionChannel::ECC_Pawn), TraceParams))
    {
        AActor* HitActor = OutHit.GetActor();

        if (HitActor)
        {
            UE_LOG(LogTemp, Warning, TEXT("HitActor: %s"), *HitActor->GetName())
        }

        ABaseCharacter* HitCharacter = Cast<ABaseCharacter>(HitActor);

        if (HitCharacter)
        {
            return HitCharacter;
            UE_LOG(LogTemp, Warning, TEXT("Cast successful"))
        }
    }

    return nullptr;
}


Log when I click on another player:


LogTemp: Warning: HitActor: BP_BaseCharacter_C_2

The BP_BaseCharacter is simple the BP implementation of the C++ class ABaseCharacter. What am I doing wrong that HitActor cannot be cast to ABaseCharacter?

I’ve also tried casting from HitActor->GetOwner() but this returns a nullptr

Thanks :slight_smile:


return HitCharacter;
UE_LOG(LogTemp, Warning, TEXT("Cast successful"))

it returns before logging

Well that’s embarrassing.

I think the real lesson here is don’t code after midnight. Thank you OptimisticMonkey.