Search Unity

Character Controller and Collision

Discussion in 'Editor & General Support' started by littlelingo, Feb 20, 2007.

  1. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    I have a couple avatars wandering around the scene that have CharacterControllers attached them. What I am looking to do is detect when a collision occurs between them. In a script attached to my characters I am using OnControllerColliderHit to determine if a collision occurred. Now, this seems to work with box colliders, mesh colliders and I am assuming other collider types but it doesn't seem to generate an event when the the object has a CharacterController attached to it. Now, if I attach a collider then I will get a message. For me, where it gets a bit confusing, is when I go to attach a collider to the object that already has a CharacterController it seems to recognize the controller as a type of a collider. So I guess my question is, can I use CharacterController with OnControllerColliderHit and other CharacterControllers or do I have to attach a different collider to all my characters?

    Now, I am currently using SimpleMove for the user avatar and Move for the AI avatars and so looked into CollisionFlags though the didn't seem to do a whole lot for me with other CharacterControllers and also looked into OnCollisionEnter, Stay, etc. with no real luck.

    Thanks for any insight! Regards,

    -- Clint
     
  2. ScatterBrain

    ScatterBrain

    Joined:
    Sep 23, 2006
    Posts:
    37
    This never got a response, but I'd like to know as well:
    CharacterControllers vs CharacterController does not issue an OnControllerColliderHit() ?
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,771
    It gets a OnControllerController hit instead.
     
  4. ScatterBrain

    ScatterBrain

    Joined:
    Sep 23, 2006
    Posts:
    37
    OnControllerControllerHit() ? Sorry, was that a misspelling or an undocumented feature...or did I miss something else entirely?
     
  5. Erik Harg

    Erik Harg

    Joined:
    Jul 2, 2008
    Posts:
    28
    I'm having some trouble getting two chacaterController-controlled objects to register collisions as well.

    Well, to be precise: I do get some collisions registered between my two charactercontroller objects using the OnControllerColliderHit, but not nearly as many as the visual collisions that I can see between the two...

    There seems to be a class called UnityEngine.ControllerControllerHit, and therefore it'd be logical if there was a callback function named OnControllerControllerHit that'd be called in this instance. But... I can't find any documentation.

    What information does the ControllerControllerHit class contain? Obviously not a .gameObject, .transform or .point... Something else? Anyone care to shed some light on the subject?
     
  6. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    Looks like ControllerControllerHit is a typo. What you're really looking for is ControllerColliderHit.

    More info here and here
     
  7. Erik Harg

    Erik Harg

    Joined:
    Jul 2, 2008
    Posts:
    28
    Ok. Funny that I didn't get a compiler warning when using ControllerControllerHit, then. Anyway, I have now been using the OnControllerColliderHit, both ways (both at the collider and at the collided object), but it hasn't given us the expected results, as far as registering all collisions.

    I did get a tip from AngryAnt at the IRC channel that I should try using the OnCollisionEnter instead, but it didn't seem to fare much better; i.e. I do get certain collisions detected, but my small, attacking animals are seen bouncing off the player far more often than the number of calls to any of these functions.

    I am now on holiday and have left this task to another of our guys (ToreTank), so he'll probably fill you all in if he has any bright ideas in the coming days, but so far it seems like we have to rethink using the collisions for detecting when our creature hits the player.
     
  8. toeadd

    toeadd

    Joined:
    Mar 24, 2009
    Posts:
    2
    I tried to search for awhile for an answer to this but haven't found one, so I'm sorry if I missed it.



    I am having the same problem as above:

    I have 2 objects with Character Controller's that need to collide. When the 2 touch no collision is being called.

    I have tried OnControllerColliderHit and OnCollisionEnter on both objects, but never get a message from either.

    One of the objects is doing a SimpleMove into the other and stoping, so the collision has to be happening, I just am not seeing any messages being called.

    Thanks for your time,
    Todd
     
  9. sidestepper

    sidestepper

    Joined:
    Oct 2, 2008
    Posts:
    94
    Bump

    I am having this issue and it was never fully answered here.

    I have two objects in my scene, both have character controllers. I need to get an event when they have collision. OnCollisionEnter does not fire, nor does OnControllerColliderHit fire, and OnControllerControllerHit is obviously not a real function in Unity, and even if it was, it's not firing either because I tried it, however it was interesting to be able to catch a ControllerControllerHit type variable in that function and not getting an error back saying it didn't exist as a type.

    How does one go about getting two objects both with CharacterControllers attached, to fire an event on collision?
     
  10. toeadd

    toeadd

    Joined:
    Mar 24, 2009
    Posts:
    2
    I found adding another collider to the CharacterController will trigger it.

    It will prompt you to either Replace or Add, I just added it so it has both CharacterController and a Collider.
     
  11. sidestepper

    sidestepper

    Joined:
    Oct 2, 2008
    Posts:
    94
    Thanks that did the trick. It's unfortunate that we have to add the overhead of two colliders to do this though - wonder if Unity has another way to do it or not?
     
  12. MultimediaPlusGreg

    MultimediaPlusGreg

    Joined:
    Jul 15, 2009
    Posts:
    23
    It didn't work for me. After adding a Capsule Collider in addition to having a Character Controller I started getting errors from my routines with raycasts that need to hit colliders. Looks like having two colliders confuses that function for some reason. Has anybody REALLY gotten this to work properly? It seems like a very basic operation... 2 NPCs or the Player and an NPC touch... give me some info on that! I don't get why it's so stubborn in this regard.
     
  13. sidestepper

    sidestepper

    Joined:
    Oct 2, 2008
    Posts:
    94
    I have been messing with this for the past hour now, and still cannot get an event when two charactercontrollers hit each other on a move call.

    I tinkered with the ControllerControllerHit(And no, that's not a typo in this case) as well, was able to create one and access a controller property in it, but it did not contain other things I tried like name, point or transform - it's like some kind of half implemented object, no documentation on it, if you google it you get this thread and thats it! Something tells me ControllerControllerHit is the key, but if they never finished implementing it and are not bothering to send the right message or it's just some crazy message, I've tried catching a ControllerControllerHit in various(At least 14 variations) method names, none seem to fire.

    The alternative can work, just add as a trigger, might have to raise its size a bit so it will trigger right before the two controllers hit each other. If you have some raycast issues, try putting it on a different game object parented or as a child to the one with the controller and put it on a different layer or something.

    I will continue to tinker with possibilities of actual events when two CharacterControllers hit each other, but I fear it might be a lost cause that they may finish implementing later.
     
  14. Games-Foundry

    Games-Foundry

    Joined:
    May 19, 2011
    Posts:
    632
    I can't believe that 4 years on I'm having the same issue with Unity3D 3.2.
     
  15. willyfreddy

    willyfreddy

    Joined:
    Mar 28, 2011
    Posts:
    51
    For anybody still having trouble with this, I decided to mess around with a simple scene that contained two cubes, each with a charactercontroller, with the user controlling the movement of one of them with a,s,d,f.

    I found that if I added another collider onto each of the cubes, I could register collisions between the two CharacterControllers with OnControllerColliderHit(). Unfortunately, I also found that the cube's CharacterController was registering collisions between itself and the cube's newly added collider. This made the moving cube behave VERY oddly (continually moving down and being very jittery).

    To get around that, I had to disable collisions between the CharacterController and the newly added collider:

    Code (csharp):
    1.  
    2. public class RedCubeBehaviour : MonoBehaviour
    3.  
    4. void Start() {
    5.    CharacterController controller = GetComponent<CharacterController>();
    6.    Collider collider = GetComponent<BoxCollider>();
    7.    Physics.IgnoreCollision(controller,collider);
    8. };
    9.  
    10. .
    11. .
    12. .
    13.  
    14.  
    Still, there is some jiterryness left in the cube's movement. I have no idea why this is.

    Regardless, this solution seems very hack-y to me. I cannot imagine that this is the correct way to deal with CharacterControllers colliding against each other.

    Will

    p.s. OnControllerControllerHit() is not a method contained in the Unity library.
     
    Last edited: Nov 19, 2011
    sami1592 likes this.
  16. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    427
    I've got two objects with character controllers attached - a player and an enemy.

    They both collide and I've not noticed any missed collisions. However, when using Debug.Log to report the collided with object, they both report that they collided with the floor, rather than each other. This is strange, as I am checking for sides collisions.

    I am guessing there is a problem when character controllers collide with multiple objects simultaneously.

    Does seem buggy to me.
     
  17. softwarespecial

    softwarespecial

    Joined:
    Nov 18, 2011
    Posts:
    74
    The Character Controller has a collider but it is usually small and must be adjusted. Try finding the Radius and height and ajust that tell you see the collider and have it surround your Character in the Script for the Character Controller
     
  18. BasketQase

    BasketQase

    Joined:
    Sep 12, 2005
    Posts:
    97
    I've found similar issues with ControllerColliders. In my situation I have a player-controlled bike that's avoiding cars. Hit detection works fine while the player is moving, but if the player is stopped, cars will drive right through the player without causing any collision callbacks. Seems like collision isn't detected if the ControllerCollider is not moving.
     
  19. Malik Ehtasham

    Malik Ehtasham

    Joined:
    Nov 17, 2012
    Posts:
    3
    Actually character controller only detect the collision when the character are moving .Other wise collision can not be detected.
     
  20. Littlest Teapot

    Littlest Teapot

    Joined:
    Jun 3, 2013
    Posts:
    1
    So there is a fix for this!
    What you are going to need is a BoxCollider on a GameObject added to your Enemy with the CharacterController.
    Set up 2 layers. I named mine "Player" and "EnemyHitBox". Set the Player (with the CharacterController) to "Player" and the GameObject with the BoxCollider to "EnemyHitBox".

    Now to avoid that hitbox doing anything extra head on over to Edit -> Project Settings -> Physics. Alter the Physics so that the "EnemyBoxCollider" layer will only interact with the "Player" layer.

    I also added a tag called "EnemyHitBox" to that GameObject so I could do the following within my PlayerController script...

    You now have a hitbox that will only interact with your players and can be called on via the CharacterController. Viola!
     
  21. SolutionStudios

    SolutionStudios

    Joined:
    Aug 1, 2014
    Posts:
    105
    For anyone still having the problem, there is another simple solution:
    add a capsule colider to each of the character controllers and resize them to the same size as the character controller
    tick the "Is Trigger" box on the capsule colliders
    use the "OnTriggerEnter" function to detect the collision
    Code (JavaScript):
    1. function OnTriggerEnter (collider : Collider) {
    2. if (collider.gameObject.tag == "tagOnOtherObject") {
    3. //do something
    4. }
    5. }