Hi all. Been following this tutorial. Just picking things up really.
Originally this was all working fine. I’ve followed everything exactly, apart from I didn’t add the first person meshes as I will be making a third person game.
The projectile direction was working fine until I set the collision for the projectile itself.
Now, it seems to have broken and will only fire in one direction. No matter where I am facing.
Here is the Character.cpp
#include "ROM.h"
#include "ROMCharacter.h"
#include "ROMProjectile.h"
AROMCharacter::AROMCharacter(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
// Create a CameraComponent
FirstPersonCameraComponent = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FirstPersonCamera"));
FirstPersonCameraComponent->AttachParent = CapsuleComponent;
// Position the camera a bit above the eyes
FirstPersonCameraComponent->RelativeLocation = FVector(0, 0, 50.0f + BaseEyeHeight);
}
void AROMCharacter::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("ROMCharacter!")); // Ensures this character class is being used
}
}
void AROMCharacter::SetupPlayerInputComponent(UInputComponent* InputComponent)
{
// set up gameplay key bindings
InputComponent->BindAxis("MoveForward", this, &AROMCharacter::MoveForward);
InputComponent->BindAxis("MoveRight", this, &AROMCharacter::MoveRight);
InputComponent->BindAxis("Turn", this, &AROMCharacter::AddControllerYawInput);
InputComponent->BindAxis("LookUp", this, &AROMCharacter::AddControllerPitchInput);
InputComponent->BindAction("Jump", IE_Pressed, this, &AROMCharacter::OnStartJump);
InputComponent->BindAction("Jump", IE_Released, this, &AROMCharacter::OnStopJump);
InputComponent->BindAction("FireLeft", IE_Pressed, this, &AROMCharacter::OnFire);
InputComponent->BindAction("FireRight", IE_Pressed, this, &AROMCharacter::OnFire);
}
void AROMCharacter::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);
}
}
void AROMCharacter::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 AROMCharacter::OnStartJump()
{
bPressedJump = true;
}
void AROMCharacter::OnStopJump()
{
bPressedJump = false;
}
void AROMCharacter::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 space 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
AROMProjectile* const Projectile = World->SpawnActor<AROMProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
if (Projectile)
{
// find launch direction
FVector const LaunchDir = MuzzleRotation.Vector();
Projectile->InitVelocity(LaunchDir);
}
}
}
}
Any ideas what could be causing this? Could be the blueprint itself or what?
Thanks everyone. I really appreciate the help!