Search Unity

Collision detection

Discussion in 'Scripting' started by Krisper, Apr 15, 2011.

  1. Krisper

    Krisper

    Joined:
    Mar 24, 2011
    Posts:
    20
    Hi,

    I am only new to Unity and have spent the last couple of weeks doing tutorials and reading. Now I have started my game.

    I have a character controller for walking around my level, I have added a hand object as a child of the player, it just hangs near the character body at the moment. So it moves around with the player. I have added a box collider to the hand. I have a few other objects around the place that have no physics, they just have box colliders too. I need to detect when my hand is touching one of these objects so I can pick it up.

    I get no response from the OnCollisionEnter function. If I add a rigidbody to either the hand or the other objects then I can detect the collision but the objects go crazy. I only want them to be picked up by the hand, otherwise they must stay in place.

    Is there a way to do this with collisions or do I need to do some raycasting or something like that?
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    hmmm.. make the "hand" object empty, no collider. create a rigidbody box the same size as the hand. Every Update make it's position, and rotation match the hand object, and set it's velocity and angularVelocity to zero. This way, you have a "linked" rigidbody, that never moves and has no effect on the game. It does, however, cast the OnCollision events.

    I am not for sure, but this also could be a good method of doing a "push" with a CharacterController.
     
  3. Krisper

    Krisper

    Joined:
    Mar 24, 2011
    Posts:
    20
    Thanks for the help BigMisterB. Please excuse my stupid questions, but I am only just getting the hang of Unity so I am not sure if I am doing exactly what you suggested. I removed the Box collider from the hand. I then created an empty object, added a rigid body and a box collider the same size as the hand. I added a collision detection script to this which now detects collisions. I am also setting the position and rotation of this object to the same as the hand each update.

    My problem is that the hand is a child of my player which has a character controller, so the forward movement is affected. I can move sideways and backwards OK but when I try to move forward the collision box I made is getting in the way and stopping me from going forward. Have I done what you suggested properly? And how would I stop it affecting my characters forward movement? Thanks again for the help.
     
  4. Do3D

    Do3D

    Joined:
    Aug 26, 2009
    Posts:
    50
    Collider Collider collision should be detectable without the rigidbody being attached and this is a serious problem many are facing and me.
     
  5. Krisper

    Krisper

    Joined:
    Mar 24, 2011
    Posts:
    20
    The whole rigidbody thing was causing a problem so I am going to use raycasting for now. Thanks.