Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Inaccurate capsuleCast

Discussion in 'Physics' started by Ruchir, Jun 27, 2019.

  1. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    84
    I'm having a lot of problems with setting up a kinematic character controller because of inaccurate capsuleCasts.
    The problem I'm having is that when the the capsule(Player) is far from wall it detects it (Though I'm not sure it gets the accurate points either) but when the capsule is touching the wall after it have moved, the capsule cast doesn't register the object for some reason,It isn't overlapping or something as far as I checked my code ,I'm hoping someone tell what am i missing or is there a problem with unity physics itself and there is an workaround
    This is my code :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TMT_CapsulePlacer : MonoBehaviour
    6. {
    7.     public Transform Head;
    8.     public Transform Bottom;
    9.     public CapsuleCollider m_Collider;
    10.     public Vector3 Direction = Vector3.forward;
    11.     public KeyCode ActivationKey = KeyCode.W;
    12.     public Rigidbody m_Body;
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.        
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     private void Update()
    21.     {
    22.         if(Input.GetKeyDown(ActivationKey))
    23.         {
    24.  
    25.            RaycastHit hit;
    26.             if(Physics.CapsuleCast(Head.position , Bottom.position , m_Collider.radius , Direction , out hit, Direction.magnitude))
    27.             {
    28.                 transform.position += Direction.normalized * hit.distance;
    29.                 Debug.Log("Hit position = " + hit.point);
    30.             }else
    31.             {
    32.                 transform.position += Direction;
    33.                 Debug.Log("No Obstacle Detected");
    34.             }
    35.         }
    36.     }
    37.  
    38. }
    39.  
    (I just made this smaller version to find what exactly was the problem)

    and here is my scene setup:
    I hope someone is able to help me :)
     
  2. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    813
    I tried to use boxCast and had problems with it, so I presumed it's buggy and stopped using it.

    there's another problem mentioned with these just today
     
  3. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    84
    Any work arounds?
     
  4. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    84
    please someone help
     
  5. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    813
    you can move/enable collider in Update/FixedUpdate
    and get OnCollisionEnter/ OnTriggerEnter data
     
  6. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    84
    That doesn't work for high velocity and thin colliders:(
     
  7. MineCode27

    MineCode27

    Joined:
    Dec 27, 2013
    Posts:
    2
    if you want to work that with high velocity then you have to change collison detection on rigidbody from discrete to one of continuous, depands which one suits your needs
     
  8. dibdab

    dibdab

    Joined:
    Jul 5, 2011
    Posts:
    813
    found out that Physics.CheckBox works fine

    there is CheckCapsule too