Hello AnGaraa,
You are not assigning the hit actor, I.E HitResault->GetActor()
This is my interaction method so far for my player. I put all the line tracing into a static function library to keep everything nice and neat and callable from anywhere.
void AplayerCharacter::Interact( )
{
FHitResult hitResaults(ForceInit); //This hit resault structure our line trace will output too.
if (!UStaticFunctionLibrary::playerLineTrace(this, hitResaults))
{
//Logging here
return;
}
AActor * hitActor = hitResaults.GetActor();
if (Cast<AItem>(hitActor))
{
AItem * tmpItem = Cast<AItem>(hitResaults.GetActor());
tmpItem->Interact(this);
}
}
As you can see, I get the hit actor from the FHitResults structure: AActor * hitActor = hitResaults.GetActor();
. In my static function I also check if the hit actor is valid:
if (outResaults.GetActor()->IsValidLowLevel())
{
return true;
}
So when KingCole32 said that EActor is never assigned is because it is never initialized so you will get bugs and errors. May I suggest using the Visual Studio Debugging Tool to debug if you are fairly new to C++! Using the debugger will allow you to add break points, check values of variables and what not. Make sure you follow this Visual Studio Debugger Extension(works with UE4 and Visual Studio 2015). It will allow you to see the values of some data structures defined in Unreal Engine, such as FString.
Back on topic, All you have to do is assign the hit actor to EActor and you should be all good to go, so try this:
void AMyRaycastProjectCharacter::PerformRaycast()
{
AEnemyActor EActor;
FHitResult* HitResult = new FHitResult;
FVector StartTrace = FirstPersonCameraComponent->GetComponentLocation();
FVector ForwardVector = FirstPersonCameraComponent->GetForwardVector();
FVector EndTrace = ((ForwardVector * 5000.0f) + StartTrace);
FCollisionQueryParams* TraceParams = new FCollisionQueryParams;
if (GetWorld()->LineTraceSingleByChannel(*HitResult, StartTrace, EndTrace, ECC_Visibility, *TraceParams))
{
DrawDebugLine(GetWorld(), StartTrace, EndTrace, FColor(255, 0, 0), true);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("You Hit: %s"), *HitResult->Actor->GetName()));
//Check if hit actor is valid
if (!HitResaults->GetActor()->IsValidLowLevel())
{
//Some logging here and what not
return;
}
//Assign the hit actor to EActor
EActor = HitResults->GetActor();
EActor.ApplyDamage();
}
}
Also, may I suggest to not use a pointer for HitResult, it is simply not needed(check my example).
Cheers,