Search Unity

OnControllerColliderHit with CharacterController creates a stack overflow

Discussion in 'Physics' started by Firlefanz73, Feb 9, 2019.

  1. Firlefanz73

    Firlefanz73

    Joined:
    Apr 2, 2015
    Posts:
    1,316
    Hello,

    when two beings start to overlap, I want to simply move them away from each other a Little bit.
    It seems to work but somehow this leads to a single overflow exception sometimes.

    Code (CSharp):
    1.     void OnControllerColliderHit(ControllerColliderHit hit)
    2.     {
    3.         if (Vector3.Distance(transform.position, hit.collider.gameObject.transform.position) < controller.radius * 1.6f)
    4.         {
    5.             if (hit.gameObject.CompareTag("Monster") || hit.gameObject.CompareTag("Animal") || hit.gameObject.CompareTag("NPC"))
    6.             {
    7.                 Vector3 direction = transform.position - hit.collider.gameObject.transform.position;
    8.                 direction.Normalize();
    9.                 controller.SimpleMove(direction);
    10.             }
    11.         }
    How is there a better way? Controller is a CharacterController.

    Thanks a lot :)