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);
}
}
}
}