How to seperate camera from player rotation

I am making first person game and when I look up or down my player just straight up rotates 90°. This also makes moving impossible while looking up or down.

Is there a way to keep my player character Y rotation locker while looking up or down?

I made input in in c++:

// Fill out your copyright notice in the Description page of Project Settings.


#include "PController.h"
#include "EnhancedInputComponent.h"
#include "PCharacter.h"

void APController::OnPossess(APawn* aPawn)
{
	Super::OnPossess(aPawn);

	PlayerCharacter = Cast<APCharacter>(aPawn);
	checkf(PlayerCharacter, TEXT("Something is worng."));

	EnhancedInputComponent = Cast<UEnhancedInputComponent>(InputComponent);
	checkf(EnhancedInputComponent, TEXT("Something is worng."));


	UEnhancedInputLocalPlayerSubsystem* InputSubsystem =
		ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer());
	checkf(InputSubsystem, TEXT("Something is worng."));

	InputSubsystem->ClearAllMappings();
	InputSubsystem->AddMappingContext(InputMappingContext, 0);

	if(ActionMove)
	{
		EnhancedInputComponent->BindAction(ActionMove, ETriggerEvent::Triggered, this, &APController::HandleMove);
	}
	if (ActionLook)
	{
		EnhancedInputComponent->BindAction(ActionLook, ETriggerEvent::Triggered, this, &APController::HandleLook);
	}
	if (ActionJump)
	{
		EnhancedInputComponent->BindAction(ActionJump, ETriggerEvent::Triggered, this, &APController::HandleJump);
	}
	if (ActionRun)
	{
		EnhancedInputComponent->BindAction(ActionRun, ETriggerEvent::Triggered, this, &APController::HandleRun);
		EnhancedInputComponent->BindAction(ActionRun, ETriggerEvent::Completed, this, &APController::HandleRunRelease);
	}
	if (ActionCrouch)
	{
		EnhancedInputComponent->BindAction(ActionCrouch, ETriggerEvent::Triggered, this, &APController::HandleCrouch);
		EnhancedInputComponent->BindAction(ActionCrouch, ETriggerEvent::Completed, this, &APController::HandleCrouchRelease);
	}
}

void APController::OnUnPossess()
{
	EnhancedInputComponent->ClearActionBindings();
	Super::OnUnPossess();
}

void APController::HandleMove(const FInputActionValue& InputActionValue)
{
	const FVector2D MovementVector = InputActionValue.Get<FVector2D>();

	if (PlayerCharacter)
	{
		PlayerCharacter->AddMovementInput(PlayerCharacter->GetActorForwardVector(), MovementVector.Y*PlayerCharacter->MoveForwardRate);
		PlayerCharacter->AddMovementInput(PlayerCharacter->GetActorRightVector(), MovementVector.X*PlayerCharacter->MoveRightRate);
	}
}

void APController::HandleLook(const FInputActionValue& InputActionValue)
{
	const FVector2D LookAxisVector = InputActionValue.Get<FVector2D>();

	if (PlayerCharacter)
	{
		AddYawInput(LookAxisVector.X * PlayerCharacter->LookRightRate);
		AddPitchInput(LookAxisVector.Y * PlayerCharacter->LookForwardRate);
	}
}

void APController::HandleJump()
{
	if(PlayerCharacter)
	{
		PlayerCharacter->UnCrouch();
		PlayerCharacter->Jump();
	}
}

void APController::HandleRun()
{
	if(PlayerCharacter)
	{
		PlayerCharacter->MoveForwardRate = 2.f;
		PlayerCharacter->MoveRightRate = 2.f;
	}
}

void APController::HandleRunRelease()
{
	if (PlayerCharacter)
	{
		
		PlayerCharacter->MoveForwardRate = 1.f;
		PlayerCharacter->MoveRightRate = 1.f;
	}
}

void APController::HandleCrouch()
{
	if(PlayerCharacter)
	{
		PlayerCharacter->Crouch();
	}

}

void APController::HandleCrouchRelease()
{
	if (PlayerCharacter)
	{
		PlayerCharacter->UnCrouch();
	}

}

Go to your Character Blueprint and make sure these are not checked for Pawn

1 Like

If you just want the camera to move the character left and right and not up and down Do that ^^^ but also on the Camera Check Use Control Rotation

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.