- Home /
How could this code be optimized?
I am building a black hole gun in my game. Everything works perfectly, but it takes a pretty big hit on performance due to all the physics calculations.
Here is the script that calculates all the physics: http://pastebin.com/SGj8qxLJ
For reference, here is a gif of what it looks like: https://gfycat.com/BraveVerifiableArchaeocete
I know it's really bad to use FindObjectsOfType()
every frame, but I can't think of any other way to make sure I'm always affecting every particle system.
If you would like more clarification on how any of the code works, please ask.
Answer by Kiwasi · Dec 06, 2014 at 01:06 AM
That's a huge amount of code to process every frame.
For better performance you want to use your heavy Find functions as rarely as possible. Maintain a list of Components of the type you are interested. Have the Component itself check in in OnEnable and out in OnDestroy. This will eliminate your FindObjectsOfType call.
Run this through the profiler to find which of your various sub methods costs the most. Optimise the worst performing method.
I like your idea of having components check in and out. I'll try making an object that maintains lists of all active game objects so I can get rid of the find functions.
Answer by uuil. · Dec 05, 2014 at 09:48 PM
I'm afraid I don't feel like reading that code (too spread out!). If you can paraphrase it that'd be nice ;)
I reckon there's too much code there. This seems like a pretty simple problem.
But to address your problems without reading it:
If you're instantiating from one place, just store a reference to all the instantiated objects (or rigidbodies, see below) in the blackhole.
I don't know if this still creates a performance hit.. but avoid referencing rigidbodies and transforms with .notation lookups. These used to be crazy slow. I'd try and store references in the blackhole directly to the rigidbody. You can use "rigidbody.worldCenterOfMass" to avoid transform lookups completely.
// FOUR .notation COMPONENT LOOKUPS currentPowerup.rigidbody2D.velocity = Vector3.Lerp(currentPowerup.rigidbody2D.velocity, currentPowerup.transform.position.CalculateBlackHoleForce(outerForce, transform.position, outerRadius.radius, outerRotation), 0.5f);
I'd go for implementing something like this:
BLACKHOLE SCRIPT
Find reference to every physics component on creation. - Some physical constants. - Simple loop, applying force to every referenced RigidBody every tick.
SPAWNER SCRIPTS
When spawning a physics object check for black holes and add the RigidBody (not the object) to the black holes lists.
Ideally you'd have one custom instantiate() command which would automatically check for black-holes.
This reduces lookups considerably, and keeps the code concise, simple, and re-usable.
Your answer
Follow this Question
Related Questions
[C#] Game runs oddly slow 2 Answers
Understanding Raycast How Actually works in Unity [As Algorithm] 2 Answers
Multiple Cars not working 1 Answer
Improve Script Performance Physics 2 Answers
Distribute terrain in zones 3 Answers