Skip to content
Snippets Groups Projects
Select Git revision
  • 0d5f5bf71a15b40f111d91d1739b399cfe366707
  • 5.4 default protected
  • 5.5
  • dev/5.5
  • dev/5.4
  • dev/5.3_downgrade
  • feature/experimenttime_hack
  • 5.3 protected
  • _IntenSelect5.3
  • IntenSelect5.3
  • 4.27 protected
  • 4.26 protected
  • 5.0 protected
  • 4.22 protected
  • 4.21 protected
  • UE5.4-2024.1
  • UE5.4-2024.1-rc1
  • UE5.3-2023.1-rc3
  • UE5.3-2023.1-rc2
  • UE5.3-2023.1-rc
20 results

TurnComponent.cpp

Blame
  • David Gilbert's avatar
    David Gilbert authored
    - Removes all specialized IMCs from components
    - Adds two main IMCs to Pawn: General and Navigation
    - Adds one default for general, one for left and one for right handed navigation
    - Can be set in pawn
    2de83ac0
    History
    TurnComponent.cpp 3.17 KiB
    // Fill out your copyright notice in the Description page of Project Settings.
    
    
    #include "Pawn/Navigation/TurnComponent.h"
    
    #include "EnhancedInputComponent.h"
    #include "Pawn/RWTHVRPawn.h"
    #include "Utility/RWTHVRUtilities.h"
    
    void UTurnComponent::SetupPlayerInput(UInputComponent* PlayerInputComponent)
    {
    	Super::SetupPlayerInput(PlayerInputComponent);
    
    	if (!VRPawn || !VRPawn->HasLocalNetOwner())
    	{
    		return;
    	}
    
    	// simple way of changing the handedness
    	if (bTurnWithLeftHand)
    	{
    		RotationHand = VRPawn->LeftHand;
    	}
    	else
    	{
    		RotationHand = VRPawn->RightHand;
    	}
    
    	UEnhancedInputComponent* EI = Cast<UEnhancedInputComponent>(PlayerInputComponent);
    	if (!EI)
    	{
    		UE_LOG(Toolkit, Error, TEXT("Cannot cast Input Component to Enhanced Inpu Component in VRPawnMovement"));
    		return;
    	}
    
    	// turning
    	if (bAllowTurning)
    	{
    		// no snap turning for desktop mode
    		if (bSnapTurn && !URWTHVRUtilities::IsDesktopMode())
    		{
    			EI->BindAction(Turn, ETriggerEvent::Started, this, &UTurnComponent::OnBeginSnapTurn);
    		}
    		else
    		{
    			EI->BindAction(Turn, ETriggerEvent::Triggered, this, &UTurnComponent::OnBeginTurn);
    		}
    	}
    
    	// bind additional functions for desktop rotations
    	if (URWTHVRUtilities::IsDesktopMode())
    	{
    		EI->BindAction(DesktopRotation, ETriggerEvent::Started, this, &UTurnComponent::StartDesktopRotation);
    		EI->BindAction(DesktopRotation, ETriggerEvent::Completed, this, &UTurnComponent::EndDesktopRotation);
    	}
    }
    
    // A separate button has to be pressed, for when mouse movement should contribute to turning
    void UTurnComponent::StartDesktopRotation() { bApplyDesktopRotation = true; }
    
    void UTurnComponent::EndDesktopRotation() { bApplyDesktopRotation = false; }
    
    void UTurnComponent::OnBeginTurn(const FInputActionValue& Value)
    {
    	if (URWTHVRUtilities::IsDesktopMode() && !bApplyDesktopRotation)
    		return;
    
    	if (!VRPawn || !VRPawn->Controller)
    		return;
    
    	const FVector2D TurnValue = Value.Get<FVector2D>();
    
    	if (TurnValue.X != 0.f)
    	{
    		if (URWTHVRUtilities::IsDesktopMode() && bApplyDesktopRotation)
    		{
    			VRPawn->AddControllerYawInput(TurnRateFactor * TurnValue.X);
    		}
    		else
    		{
    			RotateCameraAndPawn(TurnRateFactor * TurnValue.X);
    		}
    	}
    
    	if (TurnValue.Y != 0.f)
    	{
    		if (URWTHVRUtilities::IsDesktopMode() && bApplyDesktopRotation)
    		{
    			VRPawn->AddControllerPitchInput(TurnRateFactor * -TurnValue.Y);
    		}
    	}
    }
    
    void UTurnComponent::OnBeginSnapTurn(const FInputActionValue& Value)
    {
    	if (!VRPawn || !VRPawn->Controller)
    		return;
    
    	const FVector2D TurnValue = Value.Get<FVector2D>();
    	if (TurnValue.X > 0.f)
    	{
    		RotateCameraAndPawn(SnapTurnAngle);
    	}
    	else if (TurnValue.X < 0.f)
    	{
    		RotateCameraAndPawn(-SnapTurnAngle);
    	}
    }
    
    
    void UTurnComponent::RotateCameraAndPawn(float Yaw) const
    {
    	const FVector OrigLocation = VRPawn->GetActorLocation();
    	FVector PivotPoint = VRPawn->GetActorTransform().InverseTransformPosition(OrigLocation);
    	PivotPoint.Z = 0.0f;
    
    	const FRotator OrigRotation = VRPawn->GetActorRotation();
    
    	const FRotator NewRotation = FRotator(0, VRPawn->GetActorRotation().Yaw + Yaw, 0);
    
    	const FVector NewLocation = OrigLocation + OrigRotation.RotateVector(PivotPoint);
    
    	VRPawn->Controller->SetControlRotation(NewRotation);
    	VRPawn->SetActorLocationAndRotation(NewLocation, NewRotation);
    }