Created
June 23, 2023 01:08
-
-
Save p30virus/8db71741b5f6d8f5f0d8766521c2f8df to your computer and use it in GitHub Desktop.
Rotate Player controller towards mouse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Sets default values | |
| AMousePlayerController::AMousePlayerController() | |
| { | |
| PrimaryActorTick.bCanEverTick = true; | |
| bShowMouseCursor = true; | |
| } | |
| void AD_PlayerController::Tick(float DeltaSeconds) | |
| { | |
| Super::Tick(DeltaSeconds); | |
| Look(DeltaSeconds); | |
| } | |
| void AD_PlayerController::Look(float DeltaSeconds) | |
| { | |
| FVector MousePosition, MouseDirection; | |
| const bool bSuccess = DeprojectMousePositionToWorld(MousePosition, MouseDirection); | |
| if ( bSuccess == true ) | |
| { | |
| const FVector ActorLoc = GetPawn()->GetActorLocation(); | |
| FVector MouseDirectionAdjusted = ActorLoc + ( MouseDirection * 1000.f ); | |
| MouseDirectionAdjusted.Z = ActorLoc.Z; | |
| FVector EndLocation = FMath::LinePlaneIntersection( | |
| MousePosition, | |
| MousePosition + ( MouseDirection * 10000.f ), | |
| ActorLoc, | |
| FVector{ 0.f, 0.f, 1.f } | |
| ); | |
| EndLocation.Z = ActorLoc.Z; | |
| FRotator DesiredRotation = UKismetMathLibrary::FindLookAtRotation(ActorLoc, EndLocation ); | |
| DesiredRotation.Roll = 0.f; | |
| DesiredRotation.Pitch = 0.f; | |
| FRotator NewRotation = FMath::RInterpTo(GetControlRotation(), DesiredRotation, DeltaSeconds, RotationInterpSpeed); | |
| SetControlRotation(NewRotation); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UInputAction; | |
| class UInputMappingContext; | |
| struct FInputActionValue; | |
| class APlayerController; | |
| class AD_PlayerCharacter; | |
| UCLASS() | |
| class MOUSE_API AMousePlayerController : public APlayerController | |
| { | |
| GENERATED_BODY() | |
| public: | |
| // Sets default values for this actor's properties | |
| AMousePlayerController(); | |
| virtual void Tick(float DeltaSeconds) override; | |
| UFUNCTION() | |
| void Look(float DeltaSeconds); | |
| UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="RotationConfiguration", meta=(AllowPrivateAccess = "true")) | |
| float RotationInterpSpeed{120.f}; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment