2
\$\begingroup\$

I have two models, my player character and her weapon. Each can be controlled independently (that is, I allow the player to aim the weapon). The object hierarchy is:

  • Character (with CharacterController)
    • Weapon
  • Block

The character collides with the block correctly (both behave as solid objects), but the weapon passes through the block. What components does the weapon (or block) need so that it also behaves as a solid object? (In other words, it must not pass through the block. The block should stop the weapon from moving.)

I have tried adding rigid bodies to both weapon and block, including enabling and disabling Kinematics on each, to no avail. When they do collide, they move through each other rather than behaving as solid objects. If I tick a few constraints, they behave as solid objects, but affect each other's movement and/or rotation. If I tick all the constraints, then they no longer behave as solid objects and move through each other again.

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The usual tricks with weapons are:

  1. Make your character's collider large enough that it contains your weapons, then simply ignore collisions between it and the weapon's raycasts.

  2. Make it appear not to go through by drawing it on top of other objects. This works for FPS type games:

    1. Put the weapon(s) on a new layer (for this example it will be named WeaponsLayer).
    2. Go the the main camera's settings, and click the culling mask (defaults to Everything), and uncheck your new layer (WeaponsLayer)
    3. Add a new camera (maybe name it Weapon Camera) and make it a child of the Transform that's holding your main camera.

    4. Change the culling mask of your new camera so that only your new layer (WeaponsLayer) is selected.

    5. Change the clear flag of your new camera to "Depth Only"
    6. Change the depth of your new camera so that it's larger than that of your main camera (the default Depth is -1, so 0 should be enough).
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .