I built a modified movement system from the Twin-Stick Shooter template, and the movement works. In particular, I made it so that the player could move in any of the four cardinal directions relative to his facing with the left stick, and rotate with the right stick.
This aspect of the system works.
In addition, I modified the system so that the player could only shoot in front of him. This is where things get dicey.
Shooting while standing still works fine. However, shooting while moving or rotating is weird. If he starts moving and then shooting, things work as expected. However, if the player first shoots and then moves, the shooting stops as soon as movement begins.
Here is how the system is implemented:
Set up Input:
void ATimeShooterPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
check(InputComponent);
// set up gameplay key bindings
InputComponent->BindAxis(MoveForwardBinding);
InputComponent->BindAxis(MoveRightBinding);
InputComponent->BindAxis(RotateRightBinding);
InputComponent->BindAction("FireGuns", IE_Repeat, this, &ATimeShooterPawn::FireShot);
}
Tick:
void ATimeShooterPawn::Tick(float DeltaSeconds)
{
const float ForwardValue = GetInputAxisValue(MoveForwardBinding);
const float RightValue = GetInputAxisValue(MoveRightBinding);
const float RotateRightValue = GetInputAxisValue(RotateRightBinding);
ATimeShooterPawn::MoveForward(ForwardValue);
ATimeShooterPawn::MoveRight(RightValue);
ATimeShooterPawn::RotateShip(RotateRightValue);
}
Move Forward (Move Right is similar):
void ATimeShooterPawn::MoveForward(float Value)
{
if ((Controller != NULL) && (Value != 0))
{
FVector forwardVec = GetActorForwardVector();
AddMovementInput(forwardVec, Value);
}
}
Rotate ship:
void ATimeShooterPawn::RotateShip(float Value)
{
if ((Controller != NULL) && (Value != 0))
{
AddControllerYawInput(Value);
}
}
Fire:
void ATimeShooterPawn::FireShot()
{
// If we it's ok to fire again
if (bCanFire == true)
{
UE_LOG(LogTimeShooter, Warning, TEXT("Shooting..."));
FVector FireDirection = GetActorForwardVector();
const FRotator FireRotation = FireDirection.Rotation();
// Spawn projectile at an offset from this pawn
const FVector SpawnLocation = GetActorLocation() + FireRotation.RotateVector(GunOffset);
UWorld* const World = GetWorld();
if (World != NULL)
{
// spawn the projectile
World->SpawnActor<ATimeShooterProjectile>(SpawnLocation, FireRotation);
}
...
I trimmed FireShot() because it’s the same as in the template except for the use of GetActorForwardVector() instead of a parameter (since I will only shoot straight forward).
Based on the UE_LOG warning, the FireShot() function does not seem to get called.