I have a problem with wall collisions.

I used this script (It uses Mirror)
And I get this result.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class PlayerScript : NetworkBehaviour

    public float speed = 10f;
    public Rigidbody rb;
    private bool onGround = true;
    private float mouseX;
    private float mouseY;
    private float moveX;
    private float moveZ;
    public Animator anim;
    public Transform head;

    private void Start()
        rb = GetComponent<Rigidbody>();
        anim = GetComponent<Animator>();
    if (isLocalPlayer){
        Cursor.lockState = CursorLockMode.Locked;

    // Update is called once per frame
    void FixedUpdate()
        if (isLocalPlayer){
        mouseX += Input.GetAxis("Mouse X") * Time.deltaTime * 180;
        mouseY += Input.GetAxis("Mouse Y") * Time.deltaTime * -180;
        mouseY = Mathf.Clamp(mouseY, -90, 90);
        moveX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
        moveZ = Input.GetAxis("Vertical") * Time.deltaTime * speed;
        anim.SetFloat("Vertical", Mathf.Abs(Input.GetAxis("Vertical")));
        transform.localRotation = Quaternion.Euler(0,mouseX,0);
        head.localRotation = Quaternion.Euler(mouseY, 0, 0);
        if(Input.GetButtonDown("Jump") && onGround)
            rb.AddForce(new Vector3(0,40,0), ForceMode.Impulse);
        anim.SetBool("Falls", onGround);

    private void OnCollisionEnter(Collision collision)
        if(collision.collider.tag == "Floor")
        onGround = true;

    private void OnCollisionExit (Collision collision)
        onGround = false;

Here's the Components and its settings:
8684763--1171062--bandicam 2022-12-24 16-58-43-845.png

I certainly feel like the issue is with your collider on the character. Try and setup the collider from start, I'd recommend using a capsule collider for a character. Let me know if this works.

This didn’t work.

That's because you're just moving your character by modifying its Transform. This "teleports" the character to the new position/rotation instantly, bypassing any collision detection. So if you teleport the character inside a wall, it will be inside the wall.

Then the physics engine detects two colliders interpenetrating and tries to resolve the situation applying impulses. But then you teleport the character again deeper into the wall, so the efforts of the physics engine are just overridden.

I can think on two possible solutions:

The first solution (CharacterController) is the easiest and most convenient for most projects. The second solution (Rigidbody) is useful when you want the controller to apply physically realistic effects to the environment (i.e. pushing boxes just by walking against them).

thanks for the solution

1 Like