Why Your Bullets Go Through Walls – Unity Tutorial

Tom McDonald
Updated on:
Bullets Go Through Walls

In my last dev log, I discussed implementing my weapon system in Diminishing Light. I thought everything was working fine, but then I noticed that bullets occasionally went through walls or targets. This phenomenon, known as “tunneling,” can be frustrating to troubleshoot when you first encounter this issue. In this tutorial, I’ll discuss Why Your Bullets Go Through Walls and my approach to resolving this problem.

Identifying the Issue

The root of the tunneling problem lies in how Unity’s physics engine handles high-speed projectiles. Factors like the projectile’s velocity, the frequency of physics updates, and the size and configuration of colliders can all contribute to missed collisions.

Specifically, bullets moving at high speeds may not register collisions with walls or objects due to the engine’s default physics settings.


Implementing Solutions

Although the issue of tunneling may appear complex, there are practical solutions that Unity developers can implement to tackle this problem.

The key is understanding the factors contributing to missed collisions and making targeted adjustments to improve detection accuracy.

This section will explore optimization techniques involving projectile speed, collision detection settings, colliders, and other configuration parameters.

With the right tweaks, you can minimize tunneling occurrences and achieve more realistic projectile behavior in your Unity games.

Projectile Speed Adjustment

Reducing the speed of projectiles is the most straightforward approach, yet it’s not always feasible due to gameplay dynamics. This requires a balance between realistic projectile speeds and the limitations of the physics engine.

Rigid Bodies and Collision Detection Modes

Unity’s Physics Engine provides several collision detection modes for Rigidbody components to handle interactions between objects in a scene.

These modes are designed for various scenarios, ensuring that collisions are detected accurately and efficiently, regardless of the objects’ speeds or the scene’s complexity.

Here’s an in-depth look at each collision detection mode available for Rigidbodies in Unity:

Discrete Collision Detection

This is the default mode for most Rigidbody objects. In Discrete Collision Detection mode, the physics engine checks for collisions only at the end of each physics update.

This method is efficient and suitable for objects moving at relatively slow speeds, or non-critical gameplay objects where occasionally missing a collision does not significantly impact the game’s mechanics or player experience.

Use this mode for static or slow-moving objects to conserve computational resources.

Continuous Collision Detection

Continuous Collision Detection mode is designed for objects more likely to cause tunneling issues due to fast movement.

Instead of checking for collisions only at the end of each physics update, this mode predicts and checks for collisions throughout the entire movement of an object during a frame.

It’s particularly useful for objects like fast-moving projectiles where missing a collision would be noticeable and detrimental to gameplay. However, it is more computationally intensive than Discrete Collision Detection and should be used judiciously.

Continuous Dynamic Collision Detection

This mode is an extension of Continuous Collision Detection and is specifically optimized for scenarios involving fast-moving objects colliding with other fast-moving objects.

Continuous Dynamic provides a more accurate collision detection by calculating and predicting collisions between dynamic objects (those affected by physics and can move) throughout the frame.

It’s ideal for high-speed gameplay elements where both objects in the collision move quickly, such as in a game with fast vehicles or characters that shoot and move simultaneously.

Continuous Speculative Collision Detection

Continuous Speculative Collision Detection is a relatively new addition that combines the benefits of Continuous modes with the efficiency of Discrete mode.

Instead of calculating actual physics interactions in every physics step, it speculates the positions of objects based on their current velocities and checks for potential collisions in advance.

This mode can significantly reduce the tunneling effect without the heavy performance cost of the other Continuous modes.

It’s best used for objects that move quickly but don’t necessarily interact with every frame of their movement, offering a balanced approach between accuracy and performance.

When to Use Each Mode

  • Discrete Collision Detection: Use for static or slow-moving objects where precision in collision detection is not critical.
  • Continuous Collision Detection: Ideal for fast-moving objects that interact with static or slow-moving objects, like a bullet hitting a wall.
  • Continuous Dynamic Collision Detection: Best for scenarios where both the collider and collidy are moving quickly and interactions are frequent and critical.
  • Continuous Speculative Collision Detection: Suitable for fast-moving objects where you want to minimize the performance impact while still preventing tunneling.

Each collision detection mode in Unity is designed to address specific challenges in game development related to physics simulation.

Choosing the right mode depends on the specific needs of your game’s mechanics, the types of objects involved, and the desired balance between collision detection accuracy and game performance.


Physics Timestep Tuning

The default Physics Timestep in Unity is set to 0.02 seconds. Decreasing this value increases the frequency of physics calculations, enhancing collision detection for high-speed projectiles.

However, it’s vital to consider the performance trade-off, as more frequent updates demand more from the processor.


Collider Adjustments

Properly sizing and positioning colliders is crucial. Enlarging a projectile’s collider can significantly improve collision detection, though it’s important to maintain gameplay balance and avoid overly large colliders that would detract from the realism or challenge of the game.


My Approach

To solve the issue of why bullets go through walls in Diminishing Light, I began by increasing the size of my projectiles‘ colliders, which immediately yielded better collision detection.

I then adjusted the projectile speed to reduce tunneling occurrences and a more frequent physics update interval by setting the Physics Timestep to 0.01.

Finally, adopting the Continuous Speculative collision detection mode for my projectiles struck the right balance between detection accuracy and game performance.

Final Thoughts

This seems to be a common issue that many developers face. In this post, I provided you with several techniques you can use to solve this issue. I hope you found this tutorial helpful until next time. Happy Game Dev.

Photo of author

Author

With over two decades of experience in technical and developer support, Tom has expertise in troubleshooting APIs. Over the years, he has built a many websites and tools with a primary focus on web development technologies including C#, ASP.NET, Blazor, HTML, and CSS. Last year, Tom starting to learn game development and is currently working on his first game "Last Stand," a zombie base defense game designed in Unity 3D.