Search Unity

Bug I have a problem with wall collisions.

Discussion in 'Physics' started by ivansburritoworld, Dec 24, 2022.

  1. ivansburritoworld

    ivansburritoworld

    Joined:
    Dec 17, 2022
    Posts:
    3
    I used this script (It uses Mirror)
    And I get this result.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. public class PlayerScript : NetworkBehaviour
    7. {      
    8.  
    9.     public float speed = 10f;
    10.     public Rigidbody rb;
    11.     private bool onGround = true;
    12.     private float mouseX;
    13.     private float mouseY;
    14.     private float moveX;
    15.     private float moveZ;
    16.     public Animator anim;
    17.     public Transform head;
    18.  
    19.     private void Start()
    20.     {
    21.         rb = GetComponent<Rigidbody>();
    22.         anim = GetComponent<Animator>();
    23.     if (isLocalPlayer){
    24.         Cursor.lockState = CursorLockMode.Locked;
    25.                       }
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void FixedUpdate()
    30.     {
    31.         if (isLocalPlayer){
    32.         mouseX += Input.GetAxis("Mouse X") * Time.deltaTime * 180;
    33.         mouseY += Input.GetAxis("Mouse Y") * Time.deltaTime * -180;
    34.         mouseY = Mathf.Clamp(mouseY, -90, 90);
    35.         moveX = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    36.         moveZ = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    37.         anim.SetFloat("Vertical", Mathf.Abs(Input.GetAxis("Vertical")));
    38.         transform.localRotation = Quaternion.Euler(0,mouseX,0);
    39.         transform.Translate(moveX,0,moveZ);
    40.         head.localRotation = Quaternion.Euler(mouseY, 0, 0);
    41.         if(Input.GetButtonDown("Jump") && onGround)
    42.         {
    43.             rb.AddForce(new Vector3(0,40,0), ForceMode.Impulse);
    44.         }
    45.         anim.SetBool("Falls", onGround);
    46.         }
    47.     }
    48.  
    49.     private void OnCollisionEnter(Collision collision)
    50.     {
    51.         if(collision.collider.tag == "Floor")
    52.         {
    53.         onGround = true;
    54.         }
    55.     }
    56.  
    57.     private void OnCollisionExit (Collision collision)
    58.     {
    59.         onGround = false;
    60.     }
    61. }
    62.  


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

    eisik_dev

    Joined:
    Nov 21, 2022
    Posts:
    3
    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.
     
  3. ivansburritoworld

    ivansburritoworld

    Joined:
    Dec 17, 2022
    Posts:
    3
    This didn't work.
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    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).
     
  5. ivansburritoworld

    ivansburritoworld

    Joined:
    Dec 17, 2022
    Posts:
    3
    thanks for the solution
     
    Edy likes this.