Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[CharacterController and OnTriggerEnter + OnTriggerExit problem]

Discussion in 'Scripting' started by nyudeb, Aug 19, 2016.

  1. nyudeb

    nyudeb

    Joined:
    Mar 20, 2015
    Posts:
    19
    Hi,

    So this is the situation :

    - I have one character with a charactercontroller and a rigidbody.

    - This character is in an environment with a huge box collider with "is Trigger" ON.

    So what I would like to do is that when my character go out of this box, the script on my environment object detect it and make the character respawning somewhere...It work great with a simple capsuleCollider on the character but with a charactercontroller the functions OnTriggerEnter and OnTriggerExit are called every frames... It's like my character is going out and in every frames...

    If anyone have a solution...

    Thanks ! :)

    - Nyudeb
     
  2. nyudeb

    nyudeb

    Joined:
    Mar 20, 2015
    Posts:
    19
    So I found the solution and that's LOL.

    So I had an UpdateCollider() function that updated my collider when I crouch and stand up.

    void UpdateCollider()
    {
    if (isCrounch)
    {
    m_controller.center = new Vector3(0, 0.55f, 0);
    m_controller.height = 1.09f;
    }
    else
    {
    m_controller.center = originalColliderCenter;
    m_controller.height = originalColliderHeight;
    }

    }

    I supposed that when you have a CharacterController and change its property continually it's like you recreate the collider that's why the trigger object detect it like its going IN and OUT every frames...

    So I just write a condition and it's done...

    void UpdateCollider()
    {
    if (isCrounch)
    {
    m_controller.center = new Vector3(0, 0.55f, 0);
    m_controller.height = 1.09f;
    }
    else if (m_controller.center != originalColliderCenter)
    {
    m_controller.center = originalColliderCenter;
    m_controller.height = originalColliderHeight;
    }

    }

    thanks you nyudeb !

    np bro !

    - Nyudeb :)