Search Unity

OnControllerColliderHit when stopped?

Discussion in 'Scripting' started by Tiles, Sep 4, 2011.

  1. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    No matter what i try i, am trapped. I have a rolling barrel and my player. Rolling barrel has a rigidbody. Player has a character collider.

    When i calculate the collision by OnControllerColliderHit from the player side then the collision does not count when the player is stopped. When i calculate the collision from the barrel then the collision does not count when the barrel is stopped. And i need both cases. Contact must kill the player.

    I tried several methods so far. I even implemented second colliders at the barrel and the player. And i am not really satisfied.

    Why is the character collider not counting collisions when stopped? What are my options? What is the proper way?
     
    Last edited by a moderator: Sep 4, 2011
  2. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    OnControllerColliderHit only gets called if you hit something during Move or SimpleMove.

    So whenever your OnControllerColliderHit is being called, your code calling move is actually triggering that So its like

    call to move ->
    if hit something OnControllerColliderHit ->
    original call to move now returns.

    Its like a sandwich.

    If you do not call move or simplemove you will not get OnControllerColliderHit messages. With character controller your fully responsible for its physics which is mostly a good thing because thats what its there for. But in this situation as long as your characters on screen and you feel neccisary for the collisions to happen find the place in your script where you are not calling CharacterController.Move or SimpleMove and just call SimpleMove(Vector3.zero); which applies gravity and should keep your character grounded, but still generate the messages you need.
     
  3. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Thanks BDev,

    That sounds like a really neat trick. Let`s have a look if i can get it to work :)

    EDIT, no luck. Does not work, unfortunately. Collision does still not count when the character is stopped.

    EDIT2: correction, the way was right, the method needed a bit finetuning though. controller.SimpleMove(Vector3.zero); failed, but controller.SimpleMove(Vector3(0.00000000000001, 0,0)); works like charm. There needs to be a very slight movement at all in x or z direction. y direction will not work. My current value is small enough that the movement is not visible even after minutes, but big enough that the collider nevertheless moves, and detects collisions.

    Thanks for your help :)
     
    Last edited by a moderator: Sep 5, 2011
  4. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Slight improvement. Since it is recommend to call either move or simple move just once per frame, and the above method uses both, i simply add a moveDirection.x -= 0.000000000001* Time.deltaTime; before my controller.Move(moveDirection * Time.deltaTime); tag. Does the same job, and i am back at just one move call per frame :)