How to add collisions to player movement?

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.

Please can someone help me.

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;
    }
}

You will need a collider and a rigidbody to have the standard collisions. Right now, your script is for Transform based movement, which ignores collisions,

 transform.position = newPosition

instead of Rigidbody inside of FixedUpdate(),

 m_Rigidbody.MovePosition(transform.position + inputs * Time.deltaTime * speedModifier);

so simply adding a collider won’t do the whole trick.

If you use rigidbody.MovePosition, collisions will be taken into account: Unity - Scripting API: Rigidbody.MovePosition

As for your direction issue, there is a line that moves just along the x/z axis here:

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

You can allow X orientation when underwater instead of setting it to 0.