I Want to understand the weight of my code better. Experienced Programmers?

Currently I have a bullet script that is attached to a bullet prefab which is spawned by the gun which holds the variables the bullets will use. Each bullet is operating off its own instance of the bullet class. The class finds the player using the player tag, then finds his weapon class and grabs the stats of the bullet. It then loads those stats into the declared variables inside of the bullet class, and the class uses those variables for the rest of its life.

The bullets sends out a raycast at a distance based on bullet speed and time.Delta Time, checks for hits, if it hits, it checks for what tag, then it checks what it should do based on its variables and reacts accordingly. If it does not hit, it translates to the spot the ray ended. If the projectile is explosive it contains a link to a explosion prefab which it instantiates at the location. That explosion prefab gets its data from the Gun Class the same way the bullet gets this information.

I also currently have most everything the bullet does in the Update function. I had to do this because when I attempted to place this in the FixedUpdate function it causes severe slowdown, I guess because 100’s of bullets are calling their functions at the exact same time. I imagine there has to be a better way, but I am not sure what it is right now.

Can anyone who is very well read on coding in C# and code in general help me understand the best practices for opening up some more overhead?

My system handles 100’s to 1000’s of bullets at once, all of which can have any number of properties that they, or whatever is controlling their graphical representation must be aware of. It is not running slow, but a lot of my future code is going to be done in the same way, but I feel it could be cleaner if my design was a bit more informed.

Can you explain why you would have 100-1000 bullet alive in the same frame?

If they do raycast, they must reach their target in a single frame. So why so many alive?

Raycast are notoriously expensive to perform. 100 per frame is too much.

As LightStriker said, RayCasts tax your systems performance extremely. It’s often best to use layer masks in order to reduce the processing power required. Also, if your RayCasts collide with objects which have mesh renderers, it is even more expensive to perform (emphasis on more).
For better understanding, I’d suggest you to go through this article: http://unity3d.com/learn/tutorials/modules/intermediate/physics/physics-best-practices

Hope this helps. :slight_smile:

So you have visual moving bullets and not just instantaneous raycasts, right? Yet you’re using raycasts to check for collisions a short distance in front of the bullets for every movement step? That sounds very costly. You’d probably be better off using colliders and OnTriggerEnter for bullet hits, although if the bullets are moving too fast they might warp through objects, which your raycast method does prevent…

Using physics bullets does not do what I want it to do, 100’s alive at once because although i am using raycasts as a form of hit detection, I am not doing Call of Duty bullets. These are actual projectiles. As for why so many, I built a random weapon generator that can put out an extremely wide range of weapons.

Colliders and On Trigger Enter are simply too inaccurate. If I could trust them 99.9% of the time I would, but since I cannot…

If you have ever played Earth Defense Force, that is the style of the weapons I am building. If there is a way to avoid sacrificing accuracy while still gaining power I am all for it.

Also How can I use a mask to increase performance. Currently my bullets are masked against each other. I will likely make them ignore all objects that are not enemies or geometry.

So the way I understood the article is… Add a Rigidbody to objects and make them kinematic. using Update is actually the best way to go, downside is as the framerate drops the raycasts will become longer which will make them even more taxing.

I am going to make all my objects kinematic and see if this reduces or increases the fps.

well currently even with 1330 bullets on screen at once I only drop down to 22 FPS on a laptop. I imagine I will control the number a bit more 12 shots per second of 55 bullets with a turret next to me pumping out the same is a bit ridiculous. I don’t really notice a increase in speed from making it a kinematic body, but I guess it has to be the right environment. I wish that could be elaborated on.

I did learn that I will need to use primitive colliders rather than simple mesh colliders. That alone will save me a ton of speed.