Search Unity

Collision without collision. How?

Discussion in '2D' started by mibix, Nov 17, 2013.

  1. mibix

    mibix

    Joined:
    Nov 16, 2013
    Posts:
    6
    I have 2 physics bodies. The man and the bullet. The bullet must fly through the man and not collide with him, but I want catch the moment when they contact. And bullets must collide with other bodies on the scene. How I can do this?
     
  2. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
  3. mibix

    mibix

    Joined:
    Nov 16, 2013
    Posts:
    6
    The bullet must react with other object except the man. And man shold react with other objects too.
     
  4. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    By react, I mean bounce off of. OnTrigger functions are invoked if a collider enters another collider flagged as a trigger (it's similar to OnCollision in that respect).

    For example, if your bullet is flagged as a trigger, it will pass through objects, but OnTriggerEnter will detect the collision, so you can perform what actions you need to, whether that's destroying the bullet, removing health from the target, spawning particles or simply ignoring the collision (I.e. between man and bullet) altogether.

    You can also put objects on separate layers, I.e. Player, Bullets, Enemies ... And filter collisions with masking in the Physics Manager.
     
    Last edited: Nov 17, 2013
  5. mibix

    mibix

    Joined:
    Nov 16, 2013
    Posts:
    6
    I know how works triggers and layers. But my bullet can't be a trigger - it must bounce from walls, fly through the man, but "react" with him.

    As variant I can use raycast or something like it. But I want use physics collisions, like I did it on actionscript with box2d.
     
    Last edited: Nov 17, 2013
  6. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Layers are your friend here i would say. You can define what layer can collide with what layer. So put bullet and player at different layers and disable collisions between them. You could then use raycast instead a collision to trigger if the bullet has hit the player.
     
  7. Pix10

    Pix10

    Joined:
    Jul 21, 2012
    Posts:
    850
    As attached.

    Put a second collider marked as trigger, on a layer that your bullet can interact with, as a child of the man. Put the man's main collider on a layer that can't interact with the bullet.
     

    Attached Files:

    Last edited: Nov 17, 2013
  8. mibix

    mibix

    Joined:
    Nov 16, 2013
    Posts:
    6
    Exactly. I forgot about children! Thank you!