- Home /
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);
}
}
Answer by b_chrome · Feb 24, 2014 at 07:13 AM
Make sure your player has a collider then you can either:
Use physics by adding a rigid body to the player and then use forces to move it
Add a rigid body and an OnCollisionEnter method which puts the character back to it's previous position if it hits something
Use a CharacterController and move it using Move or SimpleMove
Method 3 is probably the easiest.
Thank you so much this was very helpful. I used the 1st method you suggested and it worked great.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134534im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Enemy Follow Disable Y Axis 1 Answer
LookRotation freaks out.. 2 Answers
smooth follow only on X and Y 1 Answer
MouseLook - Min & Max Problem 2 Answers