First person shooter tutorial pitch doesn't work

Hey. I was trying to finish first person shooter tutorial from wiki. My mouse control was working before adding skeletal meshes and other components but now only the pitch rotation of the camera doesn’t work. While shooting projectiles it gets the rotation of pitch and i can’t find the reason of this problem. If someone can help i’ll be happy. Here is my character.cpp

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

#include "FirstPersonShooter.h"
#include "FPSCharacter.h"
#include "FPSProjectile.h"


AFPSCharacter::AFPSCharacter(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	FirstPersonCameraComponent = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
	FirstPersonCameraComponent->AttachParent = CapsuleComponent;

	// Position the camera a bit above the eyes
	FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 50.f + BaseEyeHeight);

	// Create a mesh component that will be used when being viewed from a 1st person view (when controlling this pawn)
	FirstPersonMesh = PCIP.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("FirstPersonMesh"));
	FirstPersonMesh->SetOnlyOwnerSee(true);
	FirstPersonMesh->AttachParent = FirstPersonCameraComponent;
	FirstPersonMesh->bCastDynamicShadow = false;
	FirstPersonMesh->CastShadow = false;

	Mesh->SetOwnerNoSee(true);
}

void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* InputComponent)
{
	// Set up game play key bindings
	InputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
	InputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
	InputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
	InputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::OnStartJump);
	InputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::OnStopJump);
	InputComponent->BindAction("Fire", IE_Pressed, this, &AFPSCharacter::OnFire);
}

// Debug message on screen
void AFPSCharacter::BeginPlay()
{
	Super::BeginPlay();

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("We are using FPSCharacter!"));
	}
}


// Handle moving forward/backward
void AFPSCharacter::MoveForward(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f))
	{
		// Find out which way is forward
		FRotator Rotation = Controller->GetControlRotation();

		// Limit pitch when walking or falling
		if (CharacterMovement->IsMovingOnGround() || CharacterMovement->IsFalling())
		{
			//Rotation.Pitch = 0.0f;
		}

		// Add movement in that direction
		const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
		AddMovementInput(Direction, Value);
	}
}

// Handle moving right/left
void AFPSCharacter::MoveRight(float Value)
{
	if ((Controller != NULL) && (Value != 0.0f))
	{
		// Find out which way is right
		const FRotator Rotation = Controller->GetControlRotation();
		const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);

		// Add movement in that direction
		AddMovementInput(Direction, Value);
	}
}

void AFPSCharacter::OnStartJump()
{
	bPressedJump = true;
}

void AFPSCharacter::OnStopJump()
{
	bPressedJump = false;
}

void AFPSCharacter::OnFire()
{
	// Try and fire a projectile
	if (ProjectileClass != NULL)
	{
		// Get the camera transform
		FVector CameraLoc;
		FRotator CameraRot;
		GetActorEyesViewPoint(CameraLoc, CameraRot);

		// MuzzleOffset is in camera space, so transform it to world before offsetting from the camera to find the final muzzle position
		FVector const MuzzleLocation = CameraLoc + FTransform(CameraRot).TransformVector(MuzzleOffset);
		FRotator MuzzleRotation = CameraRot;
		MuzzleRotation.Pitch += 10.0f; //Skew the aim upwards a bit
		UWorld* const World = GetWorld();
		if (World)
		{
			FActorSpawnParameters SpawnParams;
			SpawnParams.Owner = this;
			SpawnParams.Instigator = Instigator;

			// Spawn the projectile at the muzzle
			AFPSProjectile* const Projectile = World->SpawnActor<AFPSProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
			if (Projectile)
			{
				// Find launch direction
				FVector const LaunchDir = MuzzleRotation.Vector();
				Projectile->InitVelocity(LaunchDir);
			}
		}
	}
}

Hi theknoppix, try putting;

FirstPersonCameraComponent->bUsePawnControlRotation = true;

in the constructor

I tried it now but it didn’t work robbiecooper. Actually cameracomponent should be getting rotation because lookup inputs from my mouse affects project shooting direction, but not camera rotation.

What version of the editor are you using and are you getting any error messages?

I’m using 4.5 version and doesnt get any error message. The only problem is my mouse lookup affects ShootDirection, but not to camera pitch anyway.

When i deleted BP_FPSCharacter and re-created it everything seems work fine and problem solved. What could be the reason of this?

When you change components in a blueprints parent class, it can happen that the blueprint gets broken.

Didn’t think it can be a problem for my pitch control. Thanks for the answers anyway :slight_smile:

I also have this problem.

Deleting and recreating the blueprint didn’t solve it for me.

The tutorial worked just fine before, but since I updated to 4.5 today this part doesn’t work anymore. :confused:

First i tried to comment every other function and bind action calls and tried to recompile. I got few crashes before getting it worked. After that i deleted and re-created my BP_FPSCharacter and started to uncomment everything one by one and it worked for me.

The commenting stuff unfortunately didn’t work out for me.

I got it working by creating a new 4.5 First Person c++ Project and copying the needed code.
This is how the character constructor looks now:

    	// Create a CameraComponent	
    	FirstPersonCameraComponent = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
    	FirstPersonCameraComponent->AttachParent = CapsuleComponent;
    	FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 50.f + BaseEyeHeight); // Position the camera
    	FirstPersonCameraComponent->bUsePawnControlRotation = true;

I added " FirstPersonCameraComponent->bUsePawnControlRotation = true;" before, but it didn’t work.

In the FPSCharacter.h I changed

	//first person camera
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
		TSubobjectPtr<UCameraComponent> FirstPersonCameraComponent;

to

	//first person camera
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
		TSubobjectPtr<class UCameraComponent> FirstPersonCameraComponent;

(I simply added “class”)
Now it works for me.

I think it may be something to do with things becoming deprecated in the switch from 4.4 to 4.5. If you delete and recreate the BP, you’re resolving any conflicts between the old version and the new.

Actually i created project on 4.5. I think that bug or conflict still continues even we have hot reload feature.

Maybe its an easy question but what is the difference between using template type with and without class specifier? I’m an artist and trying to learn c++ :slight_smile:

That may actually deserve its own question. I’ve used both with and without all over the place and, so far, have not seen any difference. Presumably you’d be giving more info to the compiler…

Oh my god. I just added some new code for other stuff and restarted editor and that problem started to repeat again. Can that be a bug?

Can you post your header file?

This solved my problem!

I don’t know why putting:
FirstPersonCameraComponent->bUsePawnControlRotation = true;
didn’t work but this worked for me. Thanks robbiecooper.

Cool, well maybe put it in an answer and accept your own answer, for anyone coming across this thread. I guess the BP setting was overriding what was in the C++, or something…

Yea probably that’s the reason.Thanks for your time.
If you just paste it here i can accept it as answer.