Player goes through the walls

Hello,
My player goes through the walls despite having rigidbody and me trying everything. Please help! I have attached my script and components attached to my player below.

using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    Animator animator;
    Rigidbody rb;
    CapsuleCollider capsuleCollider;

    private float speed;
    public float WalkSpeed = 0.02f;
    public float RunSpeed = 0.06f;
    public float LeftRightMovementSpeed = 0.02f;




    public bool isGrounded;



    // Start is called before the first frame update
    void Start()
    {

        animator = gameObject.GetComponent<Animator>();
        rb = gameObject.GetComponent<Rigidbody>();
        capsuleCollider = gameObject.GetComponent<CapsuleCollider>();


    }


    void OnCollisionStay()
    {
        isGrounded = true;
    }
    void OnCollisionExit()
    {
        isGrounded = false;
    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            rb.AddForce(Vector3.up * 300);
            isGrounded = false;
        }


        float z = Input.GetAxis("Vertical") * speed;
        float y = Input.GetAxis("Horizontal") * LeftRightMovementSpeed;
        transform.Translate(y, 0, z);
        transform.Rotate(0, 0, 0);

        if (Input.GetKeyDown(KeyCode.S))
            rb.AddForce(Vector3.back * 10);


        if (Input.GetKey(KeyCode.LeftShift))
        {
            if (Input.GetKey(KeyCode.W))
            {
            }
            else
            {

            }
            speed = RunSpeed;
            LeftRightMovementSpeed = RunSpeed;
        }
        else
        {
            if (Input.GetKey(KeyCode.W))
            {
               
            }
            else
            {
                
            }
            speed = WalkSpeed;

            if (Input.GetKey(KeyCode.A))
            {              

                LeftRightMovementSpeed = WalkSpeed;
                                
            }

        }
    }
} 

Does your player move fast? If it does, using discrete means it only checks every update if it is overlapping with something.

In this case, use continuous instead. This will calculate if at any point between frames that the object would’ve collided with something, like walls.

If you still have questions, feel free to ask.

Transform.Translate will just teleport your object in the direction specified by the input parameter. There are no collision checks done on this move.

If you want collision to be checked, then you should use the movement vector you calcualted to change the rigidbody velocity. When an object is moved by the rigidbody, then Unity will check for collisions and physics interactions.

Hello,
Thanks for the response!
@randomUsrname I have already tried all the Collison modes, didn’t solve the issue.
@dstears How am I supposed to achieve this, I am not to sure…

Thanks

Something like this is what they mean:

rigidbody.velocity = Vector3

Change rigidbody to variable of rb you set, then Vector3 to the translate movement you have added in the translate.

Feel free to ask questions

Hey,
I tried it but It didn’t work. The code is giving error! See attached:

What I mean is something like this:
rb.velocity = new Vector3(y, 0, z);
This code sets the rigid body velocity to a vector3 value, xyz.
This code should be alright to copy and paste in, replacing the line of the screenshot with the error.
Let me know if you have more questions.

The issue you’re experiencing with your player going through walls despite having a Rigidbody component is likely due to the transform.Translate method being used in the Update() method. This method moves the GameObject based on its transform’s position relative to its parent. Since your GameObject has a Rigidbody component, it should be moved using the Rigidbody.MovePosition method instead.

Here’s an updated version of your code that uses Rigidbody.MovePosition:

void Update()
{
//......
    float z = Input.GetAxis("Vertical") * speed;
    float y = Input.GetAxis("Horizontal") * LeftRightMovementSpeed;

    Vector3 pos = rb.position;
    pos += new Vector3(y, 0, z);
    rb.MovePosition(pos);
//.....
}

This should fix the issue with your player going through walls.