Search code examples
collision-detectionunity-game-engine

Unity3D First Person Controller Collision detection


I am trying to have a First Person Controller collide with a cube. Both the cube and controller have a rigidbody applied, with Is Kinematic set to false, they are physically colliding, but none of my functions are being called. (I have included all functions we tried to get this working with.)

I have a second cube configured to be a trigger, which works. Both my lecturer and myself don't have any idea why it isn't working.

The code applied to the cube is as follows:

var green : Material;

function OnControllerColliderHit () {
    print("OnControllerColliderHit");
    this.gameObject.renderer.material = green;
} 

function OnCollisionEnter(){
    print("OnCollsion");
    this.gameObject.renderer.material = green;
}

function OnCollisionStay(){
    print("OnCollsionStay");
    this.gameObject.renderer.material = green;
}

function OnTriggerEnter(){
    print("OnTriggerEnter");
    this.gameObject.renderer.material = green;
}

Here is a screenshot of my project setup: https://i.sstatic.net/Y4HED.png

Also, I am getting this problem in the Windows version of unity, I have created a fresh project to do this in both times.

** EDIT: ** I forgot to mention that the functions are called if the cube hits anything else (Such as the plane if the cube's gravity is turned on), which makes me believe there is something I'm missing from the First Person Controller.


Solution

  • I found my problem. I needed to add a capsule collider to the First Person Collider. It needed to be added, not replaced, as one of the scripts depends on another, which on second thoughts isn't doing its job.

    The capsule collider needed to be a little bigger than the Controller, and the Cube's rigidbody Collision Detection had to be set to Continuous Discrete.

    Probably not the best solution, but it works.

    https://i.sstatic.net/Ii8Ru.png