How Do You Make A Character Move On The X and Y Axis and Not Run Thought Walls?

I am trying to make a maze game where the character moves along the x and y axis, but with the script that I have the character moves right thought the walls(which defeats the purpose of a maze game). How do I make my character move on the x and y axis without running though walls.

Here is the script that I am using:

function Start () {

}

function Update () {

if (Input.GetKeyDown(KeyCode.A)){
	transform.Translate(Vector3.left * Time.deltaTime, Camera.main.transform);
	}
if (Input.GetKeyDown(KeyCode.D)){
	transform.Translate(Vector3.right * Time.deltaTime, Camera.main.transform);
	}
if (Input.GetKeyDown(KeyCode.W)){
	transform.Translate(Vector3.up * Time.deltaTime, Camera.main.transform);
	}
if (Input.GetKeyDown(KeyCode.S)){
	transform.Translate(Vector3.down * Time.deltaTime, Camera.main.transform);
}
}

Make sure your player has a collider then you can either:

  1. Use physics by adding a rigid body to the player and then use forces to move it
  2. Add a rigid body and an OnCollisionEnter method which puts the character back to it’s previous position if it hits something
  3. Use a CharacterController and move it using Move or SimpleMove

Method 3 is probably the easiest.