,How do I set up collision so the player can not pass through a wall?

Hi there, I am new to Unity and I am learning it in College and have a project. I have added movement to the character and collision detection so the character knows when it has hit a wall. I am unsure of what to do now and I can not find much online that helps me. I want the character to stop at the wall and then move away when the player hits the movement keys. Any help or advice would be appreciated.

public class MoveChar : MonoBehaviour {
[SerializeField]
float moveSpeed = 4f;
public MoveChar movement;

Vector3 forward, right;


void Start () {
  

    forward = Camera.main.transform.forward;
    forward.y = 0;
    forward = Vector3.Normalize(forward);
    right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
}


void FixedUpdate () {
    if (Input.anyKey)
        Move();

}

void Move()

     {
Vector3 direction = new Vector3 (Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");

Vector3 heading = Vector3.Normalize(rightMovement + upMovement);

transform.forward = heading; 
transform.position += rightMovement;
transform.position += upMovement;

}

void OnCollisionEnter (Collision collisionInfo)
{

if (collisionInfo.collider.tag == “Cube”)
{
Debug.Log(“Collision decected”);
}
}
``}

,I am new to Unity and I am struggling to add collision to my project. I have set it up so the character knows when it hits a wall but I am unsure of what to add to get the character to stop at the wall and move when you move away from it.

public class MoveChar : MonoBehaviour {
[SerializeField]
float moveSpeed = 4f;
public MoveChar movement;

Vector3 forward, right;


void Start () {
  

    forward = Camera.main.transform.forward;
    forward.y = 0;
    forward = Vector3.Normalize(forward);
    right = Quaternion.Euler(new Vector3(0, 90, 0)) * forward;
}


void FixedUpdate () {
    if (Input.anyKey)
        Move();

}

void Move()

     {
Vector3 direction = new Vector3 (Input.GetAxis("HorizontalKey"), 0, Input.GetAxis("VerticalKey"));
Vector3 rightMovement = right * moveSpeed * Time.deltaTime * Input.GetAxis("HorizontalKey");
Vector3 upMovement = forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalKey");

Vector3 heading = Vector3.Normalize(rightMovement + upMovement);

transform.forward = heading; 
transform.position += rightMovement;
transform.position += upMovement;

}

void OnCollisionEnter (Collision collisionInfo)
{

if (collisionInfo.collider.tag == “Cube”)
{
Debug.Log(“Collision decected”);
}
}
}

Just to give you a visual idea of what everyone else is saying:


Rewrite your entire script to just

using UnityEngine;

public class MoveChar : MonoBehaviour
{
    [SerializeField] float moveSpeed = 4f;

    Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        Vector3 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        rb.velocity = direction * moveSpeed;
    }
}

Then add a rigidbody to your player and make sure gravity is turned off.
[132588-rb.jpg|132588]
And that should solve your problem.
[132587-collision.gif|132587]

hello @pako pointed some of your issues but not all. first everyone who starts with unity have this issue, understanding the update and fixedupdate. the fixedupdate is called every x time, and the update is called every frame. so what this means? in fixedupdate you should nmot be capturing inputs since some of the input events will be lost (as it might be fired the event in a frame that fixedupdate wasnt) for collisions you only need the box collider as pako pointed, but for getting the collision messages you need atleast 1 object with a rigidbody. will this solve your error? no. people at the start get confused and dont understand when to move the objects using transforms and rigidbodys, if you are moving the object with the transform as in your code, you are just teleporting the object to another posuition, so ofc it will enter walls and whatever and get pushed out, since you are teleporting, you need to use the rigidbody for movement if you want collision detections (you can use the velocity/addforces/setvelocity and others to move the object) just make sure you catch inputs in the update, yoiu save the value of the inputs and use it in the fixedupdate (your move method) but applying forces to the body.

Add a collider to both objects. Add a Rigidbody to your player object.