-2
\$\begingroup\$

I'm making a copy of the game "Paper-io.io" as an exercise, since i'm new to Unreal Engine and C++. Actually i'm having trouble making the Pawn/Player move like it should in the real game. When the game begins the Player moves in a way that could be forward, backward, left, right, whatever (it is randomly choosen) without pressing any button (WASD). If the Player doesn't press any button in the meanwhile, he'll end to hit a wall, so when he press a button/key that is not the direction the player is going, will turn and will go to that way until he'll press another button to turn that is not the opposite one, because he cannot go backward if he is going forward, and same happens if he goes left can't go right, and so on. Any idea? So far, i tried to use BindAction but the attached method will be called just on press/release button/whatever.

this is just an example of what i've done but as i said, it is not called everyframe:

void APlayerTile::MoveX()
{   
    FVector NewLocation = FVector(PlayerLocation.X, PlayerLocation.Y - Speed, 26.0f);
    this->SetActorLocation(NewLocation);
    PlayerLocation = NewLocation;
}
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

For people who've not played paper.io, it operates like Snake. In order to achieve this type of movement, you need to separate your every-frame movement from player input.

In order to do this, you'll need to store which direction your pawn should currently be moving in. You can do so in whichever way strikes your fancy -- an enum, vector, ect. In your player input actions, like the MoveX example you've given, THAT is what should be changed. Based on the input action, set that variable to the new direction.

Now you'll need to use another Pawn method, Tick. The Tick method is the one that's called every single frame. Inside of Tick, you'll want to move the pawn in the direction indicated by the variable you've stored. This way, your pawn moves every frame, and every time the player presses a button to change direction, the pawn will react and change direction.

Basic code sample by popular request!:

//inside PlayerTile.h
enum EMovementDirection {
  UP,
  DOWN,
  LEFT,
  RIGHT
  }

  private EMovementDirection CurrentMovementDirection;

  //inside PlayerTile.cpp

void APlayerTile::MoveRight() //Add more for other directions
{   
    if (CurrentMovementDirection != EMovementDirection::LEFT) 
        CurrentMovementDirection = EMovementDirection::RIGHT;
}


void APlayerTile::Tick(float DeltaTime)  //This block happens every frame
{
    FVector CurrentLoc = this->GetActorLocation();

    if (CurrentMovementDirection == EMovementDirection::LEFT)
        this->SetActorLocation(CurrentLoc + FVector(CurrentLoc.X, CurrentLoc.Y - (Speed * DeltaTime), CurrentLoc.Z));
    //etc for other directions
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You should expand your answer to include code examples if possible. \$\endgroup\$
    – Stephan
    Dec 8, 2017 at 19:21
  • \$\begingroup\$ I apprecciated the answer even if i found the solution by my own :D Thank you anyway. \$\endgroup\$ Dec 9, 2017 at 12:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .