Question about 2D character controll

Hi all,
got an interesting question I am thinking about but can’t find a solution. Maybe because I am new to Unity 2D, but anyway, I think the problem is not easy to solve.

I Implemented the following Character Controller Code:

function MoveCharacter(){
  if (Input.GetKey(KeyCode.A)){
  transform.Translate(Vector3.left * speed * Time.deltaTime);
  }

  if (Input.GetKey(KeyCode.D)){
  transform.Translate(Vector3.right * speed * Time.deltaTime);
  }

  if (Input.GetKey(KeyCode.W)){
  transform.Translate(Vector3.up * speed * Time.deltaTime);
  }

  if (Input.GetKey(KeyCode.S)){
  transform.Translate(Vector3.down * speed * Time.deltaTime);
  }

and: awsome, the character moves. The problem: I’d like to have an character that only moves ONE direction at one time. An example would be the character controll of pac man. The unique thing here: the character can’t move diagonal. And I do not want to let my character move diagonal too. Only up, down, left, right. Do you guy have an awesome idea how to implement some extra code to fix that and to let the character move like I am intending it to move?

Cheers

Please use Code tags, it makes code more readable: Using code tags properly - Unity Engine - Unity Discussions

Easiest solution: use “else”

1 Like

Mate, you are an genius! Still a bit room to polish, but it will work for the prototype. Thanks a lot for answering.

1 Like