Search Unity

Collision detection with an object attached to player?

Discussion in 'Scripting' started by fifty6, Jul 12, 2012.

  1. fifty6

    fifty6

    Joined:
    Jul 12, 2012
    Posts:
    5
    I have a flashlight in a prototype I am building. I have it working and attached to the player. The problem is that no matter what I do to it it will go through any mesh no matter if they are rigidbody and colliders or one or the other. The flashlight is a rigidbody with capsule collision. It is set to kinematic as it is the only way I can find to get it to move with the player if it has a rigidbody. Is there any way to get it to stop going into other meshes?
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    If it's kinematic, the parent player will move it and it can't react to physics. Things can bounce off of it, but it doesn't bounce. You have to make it either a child object without rigidbody (it'll import into the parent collider), or a non-kinematic separate rigidbody.
     
  3. fifty6

    fifty6

    Joined:
    Jul 12, 2012
    Posts:
    5
    I am not sure this is working. I have the flashlight as a child of Main Camera. I took the rigidbody off and now have it with only a capsule collider. It still goes through everything.

    Was this what you were suggesting to do?

    It still is not using the collision bounds of the first person controller. The player cannot walk through things but the object still goes into everything.
     
  4. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    The player can't walk through things because it has the character controller on it (im' sure you know that). If you made the character controller radius larger so that it covered the flashlight that would /should work.

    One thing you could do is place a raycast on the flash light, so that you can detect the mesh its about to go through. Then adjust the flashlight position to be between player and the random mesh.

    The same trick is used for third person camera collision, where the camera (raycast or trigger) detects that something is in between the camera and the player so the camera moves in front of it by finding the distance.