I want to make a flying character which always move forward in the direction it’s facing (basically the same behavior of the editor view) but right now it slowly change the movement direction when going in the opposite direction:
I have the following move and look code:
void AVoxelPlayer::Look(const FInputActionValue& Value)
{
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
void AVoxelPlayer::Move(const FInputActionValue& Value)
{
const FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
FRotator ControlRotation = Controller->GetControlRotation();
FVector ForwardDirection = FRotationMatrix(ControlRotation).GetScaledAxis(EAxis::X);
FVector RightDirection = FRotationMatrix(ControlRotation).GetScaledAxis(EAxis::Y);
AddMovementInput(ForwardDirection, MovementVector.Y * 2048);
AddMovementInput(RightDirection, MovementVector.X * 2048);
}
}
This is the code for one of our flying camera spectator type characters. Might help you out.
void AFreeCamCharacter::MoveForward(float value)
{
// move forward based on direction interactive camera is facing
FRotator currentRotation = Controller->GetControlRotation();
FVector currentDirectionForward = FRotationMatrix(currentRotation).GetScaledAxis(EAxis::X);
AddMovementInput(currentDirectionForward, value * MOVE_FORWARD_RATE);
}
void AFreeCamCharacter::MoveRight(float value)
{
// move right based on direction interactive camera is facing
FRotator currentRotation = Controller->GetControlRotation();
FVector currentDirectionRight = FRotationMatrix(currentRotation).GetScaledAxis(EAxis::Y);
AddMovementInput(currentDirectionRight, value * MOVE_RIGHT_RATE);
}
The look code is like this:
void AFreeCamCharacter::LookUp(float value)
{
AddControllerPitchInput(value * LOOK_UP_RATE);
}
void AFreeCamCharacter::LookRight(float value)
{
AddControllerYawInput(value * LOOK_RIGHT_RATE);
}
Hmm, it seems pretty similar to what I have right now, did you modified any property on the Character or the Character Movement?
I end up overriding the Velocity from the MovementComponent and I get the behavior that I wanted:
void AVoxelPlayer::Move(const FInputActionValue& Value)
{
// input is a Vector2D
const FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// Get the forward and right directions based on the control rotation
const FRotator ControlRotation = Controller->GetControlRotation();
const FVector ForwardDirection = FRotationMatrix(ControlRotation).GetScaledAxis(EAxis::X);
const FVector RightDirection = FRotationMatrix(ControlRotation).GetScaledAxis(EAxis::Y);
// Calculate the movement input vector
const FVector MovementInput = (ForwardDirection * MovementVector.Y) + (RightDirection *
MovementVector.X);
// Update the ControlInputVector directly
GetMovementComponent()->Velocity = MovementInput * 600.0f;
}
}
Note: it seems like it will not work in a multiplayer game, as the clients will get stuck in place
1 Like
Just these:
// set the pawn deceleration to make precise control easier
if (GetCharacterMovement())
{
GetCharacterMovement()->GravityScale = 0.0f;
GetCharacterMovement()->BrakingDecelerationFlying = GetCharacterMovement()->MaxAcceleration;
GetCharacterMovement()->SetMovementMode(MOVE_Flying);
}
Hi, thanks for replying, as I needed to step back from my last solution as I’m developing a multiplayer game and it didn’t worked with multiplayer.
I had tried to use these settings but my character still have the behavior of decelerate to then start going in the opposite direction (same behavior as shown in the video)
I had changed the Max Acceleration to 10000, the Braking Deceleration Flying to 2048 and also changed the scale values to MovementVector.Y * 10000
, and now it seems to be working properly in single player, but for some reason the behavior on the server and client are different
Ok… it seems like the SetMovementMode(MOVE_Flying);
is not being applied on the client for some reason
Ok, solved it, it was probably due to replication, I was changing it on the client side, so it got override by the server movement mode. I simply setted the Default Land Movement Mode
to Flying
and now both players are moving correctly
1 Like