Search code examples
c#unity-game-enginetriggersmesh-collider

OntriggerEnter not called playerController


I have a script that is a attached to a box, the box has two colliders on for physics and one (slightly bigger) for detecting OntriggerEnters when my player walks over it. I have a script attached to the box that does the following:

public class ColorChangeCollision : MonoBehaviour {

    private GameObject Element;
    private Color32 BaseColor, CollisionColor;
    private Material ElementMaterial;

    void Start () {
        Element = this.gameObject;
        BaseColor = new Color32(255,255,255,255);
        CollisionColor = new Color32((byte)Random.Range(0, 255),(byte)Random.Range(0, 255),(byte)Random.Range(0, 255), 255);
        ElementMaterial = Element.gameObject.GetComponent<Renderer>().material;
    }

    private void OnControllerColliderHit(ControllerColliderHit  other)
    {
        Debug.Log("collision...");
        ElementMaterial.color = CollisionColor;
    }

    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("enter");
    }

    private void OnTriggerStay(Collider other)
    {
        Debug.Log("staying..");
    }

    private void OnTriggerExit(Collider other)
    {
        Debug.Log("left...");
        ElementMaterial.color = BaseColor;
    }
}

Main problem OnTriggerEnter or OnControllerColliderHit is never called nor are the other trigger events..

See below for an image of the setup of the box, and its components:

See here for my player who should be calling the OntriggerEnter or OnControllerColliderHit function of the box:

player properties player in 3d scene

EDIT I modified all the elements to the suggestions of @Programmer. But the OnControllerColliderHit event is still not being called.. (note this function is attachted on the box )


Solution

  • There are 3 issues on how you setup your scene and your code:

    1. You are missing a Rigidbody. This should be attached to the "Cube" GameObject since it's not ok to attach Rigidbody to GameObject with a CharacterController.

    2. You have two BoxColliders attached to one GameObject ("Cube"). Do not do this. This can cause many issues including a callback function being called multiple times or non at-all.

    What to do when you need both trigger and non trigger collider?

    Create an empty child GameObject put the trigger there. You can do that.

    3. You are using CharacterController so you should remove the OnTriggerXXX callback functions. When using CharacterController, it recommended to use it's callback function OnControllerColliderHit.

    void OnControllerColliderHit(ControllerColliderHit hit)
    {
        Rigidbody body = hit.collider.attachedRigidbody;
        ......
    }
    

    4. CharacterController is not mean to be used as triggers. They are used to detect collisions.

    You have two options to make it detect triggers:

    A. Remove the isTrigger from the "Cube" Object

    Or

    B. Add a child empty GameObject under the CharacterController then add the CapsuleCollider or any other collider to it. You don't have to mark it as isTrigger since the "Cube" is already marked as so. You also shouldn't have Rigidbody attached to this child collider.

    Create a new layer named "CC" and make the CharacterController and the new child GameObject to be in the "CC" layer. Go to your physics collision matrix settings and make it so that "CC" layer cannot collide with "CC" layer. This will make sure that the CharacterController doesn't detect collsion from tis child object.

    You can now use your OnTriggerXXX callback functions to detect collision between the CharacterController and the "Cube" object.