Hello everyone.
I have this problem: in C++ I wrote the code for lock my camera on an actor that I hit and is working just fine: I press my right mouse button, then with the help of the fuction SphereTraceSingleForObjects I see that I hit the actor in my array list, but my camera don’t lock on him.
I tried adding some logs in all my if, to be sure that every single one of them is execute, and in the Output Log screen I see every single log that I wrote, so I don’t know what is the reason.
There is my code:
Player .cpp fuction
UE_LOG(LogTemp, Warning, TEXT("LockTarget Started!"));
FHitResult OutHit;
ACharacter* player = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);
camera = player->FindComponentByClass<UCameraComponent>();
if (camera == nullptr) {
UE_LOG(LogTemp, Warning, TEXT("No Camera!"));
return;
}
else {
UE_LOG(LogTemp, Warning, TEXT("CameraFounded"));
}
FRotator CameraRot = camera->GetComponentRotation();
FVector CameraLoc = CameraRot.Vector() * rangeLock;
FVector TrainerLoc = player->GetActorLocation();
FVector EndLoc = CameraLoc + TrainerLoc;
TArray<TEnumAsByte<EObjectTypeQuery>> ObjectArray;
ObjectArray.Add(UEngineTypes::ConvertToObjectType(ECC_Pawn));
TArray<AActor*> ActorsToIgnore;
ActorsToIgnore.Add(this);
UE_LOG(LogTemp, Warning, TEXT("LockTarget Boolean in action!"));
bool hasHit = UKismetSystemLibrary::SphereTraceSingleForObjects(GetWorld(), TrainerLoc, EndLoc, radiusLock, ObjectArray, true, ActorsToIgnore, EDrawDebugTrace::ForDuration, OutHit, true);
if (hasHit) {
UE_LOG(LogTemp, Warning, TEXT("First Boolean!"));
HitActor = OutHit.GetActor();
if (HitActor && HitActor->ActorHasTag(FName("CharacterMesh"))) {
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
LookRot = UKismetMathLibrary::FindLookAtRotation(camera->GetComponentLocation(), HitActor->GetActorLocation());
}
}
Player Tick fuction:
Super::Tick(DeltaTime);
if (HitActor != nullptr) {
APlayerController* PlayerController = GetWorld()->GetFirstPlayerController();
UE_LOG(LogTemp, Warning, TEXT("Hit Actor Boolean!"));
if (PlayerController) {
UE_LOG(LogTemp, Warning, TEXT("Camera Boolean!"));
camera->SetRelativeRotation(LookRot);
}
}
I also tried with SetWorldRotation but the result were the same.
There will be mistakes or useless code. I apologize, I’m still learning haha
Thanks in advice!