# How can I make the player collide with walls?

I have a code here which causes the player to move in the direction of the camera. The movements work but the player goes through walls and I’ve tried adding a box collider to the p[layer but that didn’t work. I tried adding a rigid body but it limited my movement to the x and z axis but my game is set underwater and I need the player to be able to swim up if they are facing up as well.

``````using UnityEngine;
using System.Collections;

public class Movement1 : MonoBehaviour {

/*
wasd : basic movement
shift : Makes camera accelerate */

public float sensX;
public float sensY;
public Transform orientation;

float xRotation;
float yRotation;
float mainSpeed = 5.0f; //regular speed
float shiftAdd = 8.0f; //multiplied by how long shift is held.  Running
float maxShift = 12.0f; //Maximum speed when holdin gshift
float camSens = 0.25f; //How sensitive the mouse is
private Vector3 lastMouse = new Vector3(255, 255, 255); //sets mouse near the middle
private float totalRun= 1.0f;

private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}

void Update () {
float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;

yRotation += mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);

transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
orientation.rotation = Quaternion.Euler(0, yRotation, 0);

//Keyboard commands
float f = 0.0f;
Vector3 p = GetBaseInput();
if (p.sqrMagnitude > 0){ // only move while a direction key is pressed
if (Input.GetKey (KeyCode.LeftShift)){
totalRun += Time.deltaTime;
p  = p * totalRun * shiftAdd;
p.x = Mathf.Clamp(p.x, -maxShift, maxShift);
p.y = Mathf.Clamp(p.y, -maxShift, maxShift);
p.z = Mathf.Clamp(p.z, -maxShift, maxShift);
} else {
totalRun = Mathf.Clamp(totalRun * 0.5f, 1f, 1000f);
p = p * mainSpeed;
}

p = p * Time.deltaTime;
Vector3 newPosition = transform.position;
if (Input.GetKey(KeyCode.Space)){ //If player wants to move on X and Z axis only
transform.Translate(p);
newPosition.x = transform.position.x;
newPosition.z = transform.position.z;
transform.position = newPosition;
} else {
transform.Translate(p);
}
}
}

private Vector3 GetBaseInput() { //returns the basic values, if it's 0 than it's not active.
Vector3 p_Velocity = new Vector3();
if (Input.GetKey (KeyCode.W)){
p_Velocity += new Vector3(0, 0 , 1);
}
if (Input.GetKey (KeyCode.S)){
p_Velocity += new Vector3(0, 0, -1);
}
if (Input.GetKey (KeyCode.A)){
p_Velocity += new Vector3(-1, 0, 0);
}
if (Input.GetKey (KeyCode.D)){
p_Velocity += new Vector3(1, 0, 0);
}
return p_Velocity;
}
}
``````

Using translate or directly updating the objects position will ignore all collisions, even if you have a rigidbody attached, you will still move through objects. If you want to utilise the physics system for collisions you need to use a rigidbody and move it via seeing the velocity or adding forces instead of directly translating the object or updating position.

.

Rigidbodies don’t prevent movement on any axis unless you tick the box to constrain it. I suspect you are just experiencing gravity, which you can disable.