Weapon pass through objects

Hey everyone, my gun passes through a cube. How can I make it collide with it? I have added box collider and rigidbody to both of them still it doesn’t work. “Is trigger”, “Is kinematics” and “Gravity” are set to false for both objects. Help!!

I assume you’re making an FPS or something similar?
A lot of FPS games don’t even bother with checking collision between weapons and the environment. I’m not sure exactly what your needs are, but there are a couple of options:

Option 1: Check Collisions

If an object is being moved via its transform (either with transform.position or by being the child of a moving object like your character), it won’t respond to physics, because it is being explicitly told where to go. However, the collider will still be sending collision messages. I would suggest making the collider a trigger to prevent strange interactions with physics that might feel strange to the player. Then, in a script, do something where

void OnTriggerEnter(Collider col){
     if(col.tag !- "Player"){
          //Get the gun out of the way via animation or something
     }
}

What you do to get the gun out of the way is up to you, but I imagine an animation or a lerp to a position could work. Keep in mind that the collider actually makes more sense to be spherical here, since it extends down as much as it does forward, and the side extensions will actually feel more natural and anticipatory to the player. You also might (and probably will) want to also do the next technique

Option 2: Render in Front

This is what almost every FPS does, and it works very well. I highly recommend you also implement this method.

In most FPS games, players’ weapons actually still do go through the environment, it just doesn’t look like it because they are rendered on top of the environment. This is done by assigning the gun to a separate layer, say, “Weapons,” and creating a second camera. The main camera that you use to see the environment should have its culling mask modified to exclude the “Weapons” layer. Then, a second camera should be parented to the main camera and look from the exact same spot, but have its culling mask only include the “Weapons” layer. Additionally, the second camera’s depth should be lower than the main camera’s depth, and the second camera’s clear flags should be set to “Depth Only.”

This will cause the gun to be rendered on top of the rest of the environment, so no matter how far into the environment it actually is. While this is physically wrong, it looks and feels right to the player, because people don’t just stop walking when their gun would touch something.

Option 2.5: Both

Both of these options can be done in tandem. Personally, I feel that Option 2 gives the biggest impact on how the game feels, and is also always guaranteed to work, but Option 1 is rather neat to see, and can help cover up some of the shortcomings of Option 2 when dealing with long guns that can sometimes appear a little too short when close to walls. Option 1 and 2 also work together quite nicely in that Option 1 provides a nice little animation that adds some depth to the motion on the screen, and Option 2 gives you the freedom to make these animations look nice, rather than requiring that they actually get the gun out of the way, which could end up with the gun being off the screen or in an awkward corner of it.

These techniques can be applied to things like player arms, hands, etc. as well. Good luck on your project!

How are you moving these objects? If you are using something like transform.translate or transform.position, colliders can intersect with each other.

Is it always passing through the cube?