Search Unity

Objects clipping

Discussion in 'Physics' started by BerserkerHezz, Dec 9, 2019.

  1. BerserkerHezz

    BerserkerHezz

    Joined:
    Nov 7, 2019
    Posts:
    2
    Hello everybody! I have a problem with physics (I think). I am new in unity, so I don't know how to solve it, and I was searching for some answers in google and found nothing. Basicly, my character clip through walls, doors, and everything when I walk towards the object. This is what happens (youtube.com). What could it be?

    This is the code I use:
    Code (CSharp):
    1.  
    2.     [SerializeField] float speed = 8f;
    3.  
    4.     //Private variables
    5.     Rigidbody rb;
    6.     float xDir;
    7.     float yDir;
    8.     void Start() {
    9.         //Get the component rigidbody
    10.         rb = GetComponent<Rigidbody>();
    11.     }
    12.  
    13.     void Update() {
    14.         //Update variables
    15.         xDir = 0f;
    16.         yDir = 0f;
    17.         //Get input
    18.         if (Input.GetKey(KeyCode.W)) {yDir = 1f;}
    19.         if (Input.GetKey(KeyCode.S)) {yDir = -1f;}
    20.         if (Input.GetKey(KeyCode.D)) {xDir = 1f;}
    21.         if (Input.GetKey(KeyCode.A)) {xDir = -1f;}
    22.         //Fix diagonals
    23.         if (xDir != 0 && yDir != 0) {
    24.             xDir *= Mathf.Cos(Mathf.PI/4);
    25.             yDir *= Mathf.Cos(Mathf.PI/4);
    26.         }
    27.     }
    28.  
    29.     void FixedUpdate() {
    30.         //Move the player
    31.         Vector3 move = new Vector3(xDir, 0, yDir);
    32.         rb.MovePosition(transform.position + move * speed * Time.deltaTime);
    33.     }