OnCameraUpdate - First person mesh rotating

Hello,

I’m currently busy with an issue that I cant solve alone, its just beyond me.

I already watched at this post and tried to figure out what I’m doing wrong but I cant solve it.

Environment:

  • I have a CameraSpringArm attachted to a socket and a camera attached to the arm.

    CameraSpringArm = ObjectInitializer.CreateDefaultSubobject(this, TEXT(“SpringArm”));
    CameraSpringArm->SetupAttachment(Mesh1P, TEXT(“Socket_FP”));

    Camera1P = ObjectInitializer.CreateDefaultSubobject(
    this, TEXT(“CharacterCamera1P”));
    Camera1P->SetupAttachment(CameraSpringArm);

  • I’m using the PlayerCameraManager to update my first person mesh using the ***PlayerCameraManager::UpdateCamera(float DeltaTime)

     void ***PlayerCameraManager::UpdateCamera(float DeltaTime)
     {
       // Get pawn
       auto MyPawn = PCOwner ? Cast<ATHCharacter>(PCOwner->GetPawn()) : nullptr;
       if (!MyPawn || !MyPawn->IsFirstPerson())
         return Super::UpdateCamera(DeltaTime);
     
       Super::UpdateCamera(DeltaTime);
       MyPawn->OnCameraUpdate(GetCameraLocation(), GetCameraRotation());
     }
    
  • I already replaced the function with: (Then my character is rising higher and higher)

      void ***Character::OnCameraUpdate(const FVector& CameraLocation,
                                        const FRotator& CameraRotation)
      {
        Mesh1P->SetRelativeLocationAndRotation(origin, PitchedMesh.Rotator(), false, nullptr, ETeleportType::TeleportPhysics);*/
        const FRotator RotCameraPitch(CameraRotation.Pitch, 0.0f, 0.0f);
        const FRotator RotCameraYaw(0.0f, CameraRotation.Yaw, 0.0f);
        USkeletalMeshComponent* DefMesh1P = Cast<USkeletalMeshComponent>(GetClass()->GetDefaultSubobjectByName(TEXT("PawnMesh1P")));
      
        const FMatrix CameraTranslationMatrix = FTranslationMatrix(CameraLocation);
        const FMatrix StartingTranslationMatrix = FTranslationMatrix(DefMesh1P->RelativeLocation);
        const FMatrix StartingRotationMatrix = FRotationMatrix(DefMesh1P->RelativeRotation);
        const FMatrix PitchedMatrix = FRotationMatrix(RotCameraPitch);
        const FMatrix YawedMatrix = FRotationMatrix(RotCameraYaw);
        const FMatrix ResultMatrix = StartingTranslationMatrix * StartingRotationMatrix * PitchedMatrix * YawedMatrix * CameraTranslationMatrix;
        Mesh1P->SetWorldLocationAndRotation(ResultMatrix.GetOrigin(), ResultMatrix.Rotator());
      }
    
  • With the original template code from the ShooterGame my mesh is rotating when I look up or down

     void ***Character::OnCameraUpdate(const FVector& CameraLocation,
                                        const FRotator& CameraRotation)
      {
        auto DefMesh1P = Cast<USkeletalMeshComponent>(
            GetClass()->GetDefaultSubobjectByName(TEXT("PawnMesh1P")));
        const FMatrix DefMeshLS = FRotationTranslationMatrix(
            DefMesh1P->RelativeRotation, DefMesh1P->RelativeLocation);
        const FMatrix LocalToWorld = ActorToWorld().ToMatrixWithScale();
      
        // Mesh rotating code expect uniform scale in LocalToWorld matrix
      
        const FRotator RotCameraPitch(CameraRotation.Pitch, 0.0f, 0.0f);
        const FRotator RotCameraYaw(0.0f, CameraRotation.Yaw, 0.0f);
      
        const FMatrix LeveledCameraLS =
            FRotationTranslationMatrix(RotCameraYaw, CameraLocation) *
            LocalToWorld.Inverse();
        const FMatrix PitchedCameraLS =
            FRotationMatrix(RotCameraPitch) * LeveledCameraLS;
        const FMatrix MeshRelativeToCamera = DefMeshLS * LeveledCameraLS.Inverse();
        const FMatrix PitchedMesh = MeshRelativeToCamera * PitchedCameraLS;
      
        Mesh1P->SetRelativeLocationAndRotation(PitchedMesh.GetOrigin(),
                                               PitchedMesh.Rotator());
      }