I am trying to rotate my to where the mouse is like in Halo Spartan Assault. I only want to rotate the character when I am shooting or aiming. I want the character to orient to the direction it is moving when not fire. Basically I am trying to mimic the Spartan Assault controls. I thought I had it at one point the everything fell by the waist side.
This is the affect I am looking for:
Here is my code:
void ACloneStrikePlayerController::Look(const FInputActionValue& Value)
{
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (PlayerController)
{
ACharacter* PlayerPawn = Cast(UGameplayStatics::GetPlayerCharacter(this, 0));
if (PlayerPawn)
{
// The Actor needs to be assigned to the specific actor you want to use.
// For instance, if you want to work with the player’s character, use:
ACharacter* PlayerCharacter = Cast(PlayerPawn);
PlayerCharacter = PlayerController->GetCharacter();
if (PlayerCharacter)
{
// Perform a trace under the cursor and fill the HitResult
FHitResult HitResult;
PlayerController->GetHitResultUnderCursorByChannel(UEngineTypes::ConvertToTraceType(ECC_Camera), true, HitResult);
// Now that HitResult is filled, you can get ImpactPoint
FVector ImpactPoint = HitResult.ImpactPoint;
// Calculate Target for FindLookAtRotation
FVector Start = PlayerCharacter->GetActorLocation();
FVector EndTarget = ImpactPoint - PlayerCharacter->GetActorLocation();
FRotator Target = UKismetMathLibrary::FindLookAtRotation(Start, EndTarget);
// Store current rotation
FRotator CurrentRotation = PlayerCharacter->GetActorRotation();
if (bIsShooting)
{
UKismetSystemLibrary::PrintString(this, TEXT("BSHOOTING Is Valid"));
// If shooting, interpolate the character's rotation towards the target rotation
float InterpSpeed = 500.0f;
FRotator NewRotation = UKismetMathLibrary::RInterpTo_Constant(PlayerCharacter->GetActorRotation(), Target, GetWorld()->DeltaTimeSeconds, InterpSpeed);
PlayerCharacter->SetActorRotation(NewRotation);
// Reconstruct the rotation
FRotator FinalRotation = FRotator(CurrentRotation.Pitch, NewRotation.Yaw, CurrentRotation.Roll);
PlayerCharacter->SetActorRotation(FinalRotation);
PlayerCharacter->GetCharacterMovement()->bOrientRotationToMovement = false;
}
else
{
UKismetSystemLibrary::PrintString(this, TEXT("BSHOOTING Is Not Valid"));
// If not shooting, allow the character's rotation to orient to movement
PlayerCharacter->GetCharacterMovement()->bOrientRotationToMovement = true;
}
}
}
}
}
Here are my function where I am setting and disabling bIsShooting
void ACloneStrikePlayerController::ShootStart(const FInputActionValue& Value)
{
bIsShooting = true;
APlayerBase* MyPlayer = Cast(GetPawn());
if (MyPlayer)
{
MyPlayer->Shoot();
}
}
void ACloneStrikePlayerController::ShootEnd(const FInputActionValue& Value)
{
bIsShooting = false;
}
And here is my result:
Thank you in advance