Top Down Character Movement

Hey guys!
I’m working on a game similiar to the old “champions of” series.
My problem is that I cant seem to find a way to move my character and rotate it according to it’s movement direction.
Like when I press the A key it should walk left and look forward instead of walking sidewards like it’s the case in most FPS games.

Note:This is not a movement problem, it’s a rotation problem since I’m able to let the character walk in the desired way.
The only problem is that I cant find a way to make the character always look forward(In its movement direction).

When you work out which way to move, also work out which angle to face at the same time. E.g. when you press the A key, you’re moving the player left, you can set targetDir = 180 at the same time.

Then you can either just rotate immediately to that angle, or use Mathf.MoveTowardAngle to smoothly turn over several frames.

enum Direction {
       Up,
       Right,
       Down,
       Left
}

Direction currentDirection;

void Awake()
{
           currentDirection = Direction.Up;
}
void Update()
{
            float horiz = Input.GetAxis("Horizontal");
            float vert = Input.GetAxis("Vertical");
           
            Rotate(horiz,vert);
            Move(horiz,vert);
}

Rotate(float horiz, float vert)
{
                 if (horiz < 0 )
                        SetDirection(Direction.Left);
                 if (horiz > 0)
                          SetDirection(Direction.Right);
                 if (vert < 0)
                          SetDirection(Direction.Down);
                  if (vert > 0)
                          SetDirection(Direction.Up);
}

void SetDirection(Direction targetDirection)
{
           if (currentDirection == targetDirection)
                   return;
            currentDirection = targetDirection;
            switch (currentDirection)
            {
                      case Direction.Up:
                              transform.rotation = new Vector3(0,0,0);
                              break;
                      case Direction.Left:
                              transform.rotation = new Vector3(0,-90,0);
                              break;
                     case Direction.Right:
                              transform.rotation = new Vector3(0,90,0);
                              break;
                      case Direction.Down:
                              transform.rotation = new Vector3(0,180,0);
                              break;
               }
}
1 Like

if you just want to snap to it without any animation/timedelay/lerping you could just do

transform.forward = movementDirection; // assuming you have a vector3 from the movement code
1 Like

@LeftyRight: Won’t that just move the character forward in the direction he’s facing? The OP stated he knew how to move character… he just was looking for a way to rotate the character to the left if he started walking left. So it woudln’t look like the character was strafing.