Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Performant way to calculate shotgun pellet hits

Discussion in 'Scripting' started by h0nka, Jul 16, 2021.

  1. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Hi,

    I am currently making a game where I need to do fairly accurate shotgun pellet collision checking. Some of the shotgun cartridges can contain upwards of 400 pellets. The game is supposed to run on the Oculus Quest 2 headset so performance is a big factor.

    I have considered the following approaches:
    - Raycasting each pellet (i.e. 400 raycasts pr. shot)
    - Rigidbody physics on each pellet for collision detection (although I do have to reduce the Fixed Timestep a great deal due to the high velocities of the pellets)
    - A "shape"-cast (sphere or other) for detecting collision within a certain volume (size and shape of the pellet swarm) and then (if a collision happens) running some normal distribution math to generate a set of points of the size of the pellet count to do something like Bounds.Contains to determine the amount of hits, and on which sub-colliders the hits occur

    I would love to get some input on whether there is another more performant way to handle this, or if I should go with one of the ones I have already considered.

    I am also currently trying out Hybrid ECS to see if I can get some performance benefits on doing raycasts for each pellet.

    Thanks in advance!
     
  2. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    The simple answer is you can't.

    400 of anything will be a performance hit.

    Also, shotgun shells almost never have 400 pellets, 00 buckshot ammo has 9!

    Save yourself a headache and raycast 9 or so, which would be way cheaper than rigidbodies.
     
  3. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Thank you for the reply. For skeet and clay pigeon shooting it is very common to have 400 pellets per cartridge. I am creating a shotgun hunting training simulator so some form of authenticity is required.
     
  4. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Here's a possible idea. You could also separate your raycasts into batches. Say you use 100 per batch. Run them every frame, cycling through batches and storing the results. And then, when you actually fire the shotgun, read from the stored results.

    That's 400 over 4 frames. Say you're running at 60 FPS, at most, a portion of the results will be dating 0.067 seconds.

    Upside is you mitigate the spike that comes from raycasting all at once.

    Downsides are that there's a constant strain from the raycast batch every frame, and that there will be some inaccuracies based on the delay.

    Inaccuracies will be directly connected to how fast things in your game move, I guess.
     
    h0nka likes this.
  5. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Thank you for replying. That's a good point. You definetly want performance consistency in a VR application. I'm not sure if batching the rays will be an issue in terms of accuracy. I guess the biggest issue with that would be the movement of the target over the course of those batching frames. I shall put it to the test!
     
    ADNCG likes this.