I created a papercharacter blueprint to prototype a character moving around the world. I used the add movement input node to do this. I have read that it does not update movement automatically and that I would have to do this in a tick function. However, it does just work automatically without me doing anything special in my tick to update movement.
Since that was working I started moving to C++ creating a subclass of APaperCharacter. I use the same add movement input function as I did in the blueprint, but it doesn’t move anymore. Why is the behavior different between the two versions, and how can I update movement in the C++ class?
This is the code related to the movement.
Aoverworld_player_char_c::Aoverworld_player_char_c()
{
PrimaryActorTick.bCanEverTick = true;
movement = GetCharacterMovement();
movement->GravityScale = 0;
movement->DefaultLandMovementMode = EMovementMode::MOVE_Flying;
movement->MaxFlySpeed = 185.0;
movement->BrakingDecelerationFlying = 100000000.0;
camera = CreateDefaultSubobject<UCameraComponent>(TEXT("player camera"));
camera->SetProjectionMode(ECameraProjectionMode::Orthographic);
camera->SetOrthoWidth(750.0);
camera->SetupAttachment(root);
camera->SetRelativeRotation(camera_rot);
camera->SetRelativeLocation(camera_location);
}
void Aoverworld_player_char_c::SetupPlayerInputComponent(UInputComponent* player_input_component)
{
Super::SetupPlayerInputComponent(player_input_component);
// Set up "movement" bindings.
player_input_component->BindAxis("MoveUp", this, &Aoverworld_player_char_c::moveUp);
player_input_component->BindAxis("MoveRight", this, &Aoverworld_player_char_c::moveRight);
}
void Aoverworld_player_char_c::Tick(float delta)
{
Super::Tick(delta);
is_playing = true;
setFlipbook();
moveCamera();
}
void Aoverworld_player_char_c::moveCamera()
{
camera_loc = GetActorLocation();
camera->SetWorldLocation(camera_loc);
}
bool Aoverworld_player_char_c::checkForDirectionBlockage(TSet<Edirection> dir)
{
TSet<Edirection> in = dir.Intersect(lockedDirections);
return (in.Num() == 0);
}
void Aoverworld_player_char_c::moveUp(float axis)
{
bool can_move = false;
FVector vect(0.0, 0.0, 1.0);
if (axis > 0.0)
can_move = checkForDirectionBlockage(topSet);
else if (axis < 0.0)
can_move = checkForDirectionBlockage(bottomSet);
if (can_move)
AddMovementInput(vect, (axis * movement_scale));
}
void Aoverworld_player_char_c::moveRight(float axis)
{
bool can_move = false;
FVector vect(1.0, 0, 0);
if (axis > 0.0)
can_move = checkForDirectionBlockage(rightSet);
else if (axis < 0.0)
can_move = checkForDirectionBlockage(leftSet);
if (can_move)
AddMovementInput(vect, axis * movement_scale);
}