Hello there.
I’m trying to create an optimized bullet system for shoot’em up, for my mobile game, and I need some more knowledge or advices to get it really optimized and powerfull.
What?
The goal is to create a bullet system that :
- Would be fast to render
- Would allow render sorting (Younger first, older first, no sorting) like Shuriken.
- Would be fast to collide with
- Would allow some behaviours, like bullet following stuff, or changing their direction mid-way
- Would use one or multiple sprite object(s) as rendering reference(s), using the pivot point, base texture, etc, instead of a material like Shuriken.
How?
- Fast to render
For this, I’m thinking of using a Mesh Renderer and a single mesh, keeping its reference and changing its data each frame. The bullets would be rendered all together as a single mesh. I saw point-sprite technique don’t work with Unity’s CG, is it still true?
Also, I could directly use the Graphics class as I have Unity Pro, would it be faster than using a mesh renderer, as I won’t use unity’s lighting nor shaders?
- Sorting
For this, I’m thinking of using this idea, from the cockane.com website :
http://cokane.com/shmupdevarchive/index.php?topic=2682.0
It use an array of bullet, the one holding each bullet information, and an array of bullet indice to keep trace of the active and inactive bullets.
Now, to have them rendered one over another, would simply entering them in the mesh renderer in the right order would be sufficient? Or do I need to do something different?
-
Fast to collide
For this, a simple per-frame collision check with Axis Aligned Boxes or circle will be sufficient, so I’m thinking of using the Physics2D’s OverlapCircleNonAlloc and OverlapAreaNonAlloc for each active bullet. I did a test and it is much faster than having as much Collider2D and Rigidbody2D as bullet and let Box2D do the rest, as Box2D do a lot of different operation to have precise physics with force and so on. I just need collision here. -
Allowing some behaviour
For this, I’m thinking of using C#'s delegates to be able to add a custom function that would change each bullet’s behaviour depending of their properties and the world’s state. -
Render from sprite object
Pretty simple, instead of using a single material, without indicating UVs, wich prevent me to use atlases, I’ll create each quads using the sprite’s data (position on the texture, pivot) so my bullet can correctly rotate and have trails without having a big texture for a small looking bullet with a trail. It is one of the flaw of Shuriken right now.
Multiple sprite would allow to have a looped animation for each bullet.
And you?
How would you enhance each features of the bullet system to make it even faster? Are you fond of the Graphics or Physics2D class and have the ultimate solution to optimize even further the system?