Search Unity

Character controller Collision Detection

Discussion in 'Scripting' started by amit1532, Jul 10, 2011.

  1. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    i have a player which is moving with a character controller.
    the problem is now, i want the enemy to be able to move and have gravity. i thought i should put on him character controller too, or at least a rigidbody.
    but now i have a problem, Character Controller has only " OnControllerColliderHit " which is very annoying because it wont work unless you are moving, and if the enemy touches you while you dont move, nothing happens. so no.
    there is OnTriggerEnter but i dont want to enemy to be trigger, it has gravity so it cant be trigger..
    im very frustrated about the character controller because you cant acctually detect collision, i understand " this is not physics " but why cant i use OnCollisionEnter for god sake? it is detecting collision, you can see its colliding with the floor. so why cant there be a collision detection for a character controller? i think its one of the basic things a player must have. so is there any way to get a collision between a Character controller and Rigidbody/Character Controller?
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Rigidbodies will always fire OnCollisionEnter() when they collide with a CharacterController, regardless of whether or not it is moving... so any interactions you need to code between your CharacterController player and Rigidbody enemies, you'll just have to code from the enemy perspective, not the Player (eg: a script on the enemy that says "if a player hits me" rather than a script on the character that says "if an enemy hits me")
     
  3. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    thanks for the replay,
    but why cant UT add this option to have a collision detection for a CC, because i want the enemy have many features the CC have.
    but never mind i found a work around for this. i made 2 objects for the enemy, one is the renderer with the TRIGGER which is the child of the character controller of the enemy, the parent has layer of ENEMY and player have layer of PLAYER and i disabled the collision of both layers with each other so never mind..
     
    Last edited: Jul 10, 2011
  4. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    there is another option I learned elsewhere on the forum -
    you can add another collider to your player.
    If you select "add capsule collider" to your player, Unity might say "you already have a characterController", but then you may notice there are two options in the pop-up dialog - one is to "replace" and the other is to "add" - so you can have both the characterController AND a capsule collider on your player and the capsule collider will respond to "onCollisionEnter" events, etc.. Make the collider slightly larger than the characterController capsule.
     
  5. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    but then how will the character controller collide with the floor?
     
  6. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    try it.
    the character controller capsule is what operates "as if" by physics, so when you move the controller downwards with the move function or the builtin gravity of simpleMove, it works (collides with floor).

    The added capsule collider detects when other rigidbodies collide with it, whether the character controller is doing a move or not.

    It is just another optional way of doing it. As legend411 suggested, another way is to have the enemies send a message to the player when they collide with it - which I did in a recent project also. Both of these ways seem like a fairly simple solution.
     
  7. ankiarora

    ankiarora

    Joined:
    Nov 1, 2011
    Posts:
    2
    Hello to all,

    I am using Character Controller. But when it collides other object it does not collide or we can say it does not enter in the OnControllerColliderHit function. Can you please suggest me any solution if you know as early as possible because my project is under critical situation due to this functionality.

    Thanks,
    Ankush.
     
  8. c4sh

    c4sh

    Joined:
    Nov 24, 2011
    Posts:
    1
    Hi!
    I had the same problem about the 2 CharacterControllers, tried the solution "priceap" provided, which was adding an extra capsule collider to each of the CharacterControllers so that OnCollisionEnter triggers etc.

    BUT, it's not working for me, even though I made the Capsule Collider clearly greater than the CharacterController. Is there anything I can do?
    It's curious that it works with objects (a bullet being shot to my character) that hav JUST a collider, without CharacterController.

    And, about this solution, I don't understand, when you put a function OnCollisionEnter in your script, which one of the two collider functions is calling, the one from the CharacterController (which inherits from Collider), or the one from the new added collider??? this is really wierd, hope new versions of Unity solve this...

    Thanks for any help you can give!

    Edit: I just discovered that it is actually working with CharacterController.collisionFlags, but I need to work more exactly, because I need the normal, so I can push back the main Character in the opposite direction to the enemy colliding with me! Does anyone have an idea?
     
    Last edited: May 24, 2012
  9. GlorySimba

    GlorySimba

    Joined:
    Apr 18, 2013
    Posts:
    10
    do you set the capsule collider to be a trigger?
     
  10. Panchulonz

    Panchulonz

    Joined:
    May 23, 2014
    Posts:
    2
    I found a good way and simple.
    Make a script and attach this to the gameobject that will react to the character controller
    (first, dont forget to set active the "is trigger" inside the collider of the gameobject):


    void OnTriggerEnter( Collider smash) {

    //bye bye to myself
    Destroy(this.gameObject);
    }
    }