3

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 )

FutureCake
  • 2,614
  • 3
  • 27
  • 70

2 Answers2

5

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks for the detailled explaination, but still none of the events are being called. See my updated question for the edits i made. If something is unclear let me know so i can clarify. – FutureCake Oct 27 '18 at 13:36
  • It looks like you went with isTrigger. I thought you wanted to use both trigger and non trigger collider. See #4 Edit about using just isTrigger with CharacterController . – Programmer Oct 27 '18 at 13:59
  • awesome saved my day yet again! Yeah no worries, maybe my question was a bit weird :) – FutureCake Oct 27 '18 at 13:59
  • Not weird at-all. In Unity there are many things that could cause something not functioning. There are many other reasons but the ones mentioned in my answer are just based on your current settings. For example, changing the RB to isKinematic can also cause it to stop working. – Programmer Oct 27 '18 at 14:07
0

One of them needs to have rigidbody to detect collisions.

Bhavin Panara
  • 428
  • 4
  • 11