- Home /
Rigibody and FixedUpdate
I have a case where I am adding multiple rigid bodies and taking a performance hit that accumulates as it goes through the for loop:
for (int i = 0; i < objCount; i++) { ... gameObject.AddComponent(typeof(Rigidbody)); ... }
The method is called from onGUI(). My goal is to get all iterations running with no slowdown. Will FixedUpdate have any impact here even I am not modifying the rigidbodys directly? does it even make sense to use it in this case, and if so, how?
(This is the first time I trying to use FixedUpdate, and fairly new to Unity 3D)
Thanks hg111
I don't get your setup. Are you setting a flag during OnGUI and instantiating them during FixedUpdate? I don't get what you are doing in FixedUpdate as it isn't clear from your question.
I haven't done anything with FixedUpdate yet - That's what I am trying to figure out: Can I use FixedUpdate in this case and if so, how. I was indeed thinking in line of what you are saying - setting a flag in this method and then doing the addComponent in FixedUpdate. But the docs say that you normally would use FixedUpdate on something like rigidBody.Addforce(). Will it work and be effective for addComponent?
$$anonymous$$aybe it help and clarify on how it used from ionGUI- imagine building a wall on a press of a button. But we want the bricks to assemble at a constant rate.
You should definately not create them during FixedUpdate since it may or may not run several times between each OnGUI call. You might end up with way too many components.
What I can't wrap my head around is why you're adding several Rigidbody to the same gameObject?
Answer by loopyllama · Mar 11, 2011 at 07:54 PM
use the generic version of AddComponent() to avoid the performance hit of boxing and unboxing. That will help.
Update runs every frame...roughly...which is why you have to use Time.deltaTime or calculate an elapsed time yourself.FixedUpdate runs at fixed designated intervals--a fixed time step, which is highly desireable for physics calculations. The performance cost of the tasks you are performing won't change if you place them in Update or FixedUpdate or a regular function. It is still the same workload. Ideally you want to place heavy tasks in Start or Awake. See if you can add your components there. Maybe you can add them ahead of time and simply enable them in an update or a function.
Actually, there is no boxing/unboxing being made that I can see. Besides, GetComponent(), AddComponent() is actually slower than GetComponent(Type) and AddComponent(Type). Even the string variant of GetComponent is faster than the generic one.
Update runs precisely every (rendered) frame, not roughly. FixedUpdate runs every physics frame.
@harmless: You should read up about boxing @ http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx as it only occur when you deal with value type to reference type conversions. Also, I feel you get too concerned with micro optimizations (although I do prefer the generic one for neater code). Here's another topic you might find interesting. Fast operations doesn't really matter if they are a couple times slower than another operation when placed in a real context: http://answers.unity3d.com/questions/32048/is-listt-as-fast-to-access-as-a-standard-array/32068
Hey, thanks for all the great comments so far! First - please allow me to make a correction: I wasn't adding multiple rigid bodies to the same object. I was traversing a list of objects. (child.object.rigidBody...) Sorry for the confusion. This was just sudo-code. So now I changed things a bit and ins$$anonymous$$d of doing an AddComponent inside the main loop, I am creating the objects in its own dedicated loop and forcing the rigid bodies to sleep right after they are created (for one frame). I then use rigidBody.Awake() in FixedUpdate which is triggered from the main loop with a boolean flag.
I see no difference (or improved) performance from what I had before. I am guessing the following: When the rigidBody wakes up, it collides with its' neighbors which in turn propagate and wake up the entire shebang. Every time I add a new object will wake up more and I am suspecting that is the reason for the accumulated slowdown (which btw, is not seen on the mac and only on the ipad) Can I somehow fix it with Time.deltaTime? another way?