Im trying to make sprinting in my game. I want that the player can only sprint in forward direction ( if player is pressing the W and the shift key).But if the player presses the S key and he is still holding the shift key ,I want that the player stops sprinting.
This only works on the server side, on client side doesn’t work.
Header
UFUNCTION(Server, Reliable)
void ServerSprint(bool isSprinting);
UFUNCTION(NetMulticast, Reliable)
void MulticastSprint(bool isSprinting);
bool bIsHoldingSprintButton;
Forward Axis and Sprint Input Action
void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &APlayerCharacter::MoveForward);
PlayerInputComponent->BindAction("SprintHold", EInputEvent::IE_Pressed, this, &APlayerCharacter::SprintHold);
PlayerInputComponent->BindAction("SprintRelease", EInputEvent::IE_Released, this, &APlayerCharacter::SprintRelease);
}
Bindings
void APlayerCharacter::MoveForward(float value)
{
forwardInput = value;
if(value != 0.f)
{
const FRotator rotation = GetControlRotation();
const FRotator yawRotation(0, rotation.Yaw, 0);
const FVector direction = FRotationMatrix(yawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(direction, value);
}
}
void APlayerCharacter::SprintHold()
{
bIsHoldingSprintButton = true;
if (bIsHoldingSprintButton)
{
ServerSprint(bIsHoldingSprintButton);
}
}
void APlayerCharacter::SprintRelease()
{
bIsHoldingSprintButton = false;
ServerSprint(false);
}
void APlayerCharacter::ServerSprint_Implementation(bool isSprinting)
{
MulticastSprint(isSprinting);
}
void APlayerCharacter::MulticastSprint_Implementation(bool isSprinting)
{
if(isSprinting && forwardInput > 0.f)
{
GetCharacterMovement()->MaxWalkSpeed = 600.f;
}
else
{
GetCharacterMovement()->MaxWalkSpeed = 300.f;
}
}
I tried to replicate the forwardInput variable but still doesn’t work