About to recreate a 2D bullet system for shoot'em up and other stuff. Need advices!

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?

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?

So far so good, but due to the variable count of active bullets, my mesh never have the same number of vertices/tris/uvs on it, thus I’m creating new array from different sizes each time. It triggers too much garbage collection.

How could I do it to avoid calling the ‘new’ operator each frame?

	void UpdateMesh()
	{
		m_mesh.Clear ();

		if(m_activeBulletCount <= 0)
			return;

		Vector3[] vertices = new Vector3[m_activeBulletCount * 4];
		Vector2[] uvs = new Vector2[m_activeBulletCount * 4];
		Color[] colors = new Color[m_activeBulletCount * 4];
		int[] triangles = new int[m_activeBulletCount * 6];

		for(int bulletIndex = 0; bulletIndex < maxBullets; bulletIndex++)
		{
			//...
		}

		m_mesh.vertices = vertices;
		m_mesh.uv = uvs;
		m_mesh.triangles = triangles;
		m_mesh.colors = colors;
	}

I know the maximum available bullets, should I go with a mesh of maxBullet*4 vertices and keep the unused vertices to Vector3.zero ?