- Home /
Are my kinematic rigidbodies slowing my scene down?
My scene consists of a bunch of kinematic rigidbodies which I manually move around - I am not using gravity or forces at all. I have a road object, car objects, etc - all are kinematic rigidbodies. You can also shoot bullets (also kinematic rigidbodies). The bullets are the ones with the onTriggerEnter handler - the code checks the tag of the other object and figures out the appropriate behaviour of both the bullet and what it has hit. There aren't any other collision event handlers.
I am worried that because I've placed my objects on top of each other (i.e. the car wheels are touching the road, the buildings sit on top of the road), they are generating collisions every frame and slowing everything down. The performance of my game is not great given the relatively small amount of stuff I have going on and I'm wondering if this is the cause of it. The gameObjects are constantly moving, so I have the feeling they're not falling asleep. Would this be the case? If neither of two kinematic rigidbodies have onEventTrigger handlers, does it still cause a hit on the CPU? I.e. the buildings and road are both moving at the same rate and touching each other the whole time - even though neither have onTriggerEvent handlers, would this cause slowdown?
If this is the case, is it as simple as repositioning the objects so they're not touching each other (but are very close) and then set their rigidbody sleepVelocity value to the speed that they are moving at (as they all move at a constant pre-defined speed). Is this reasonable? Or am I totally off and I need to go back to the drawing board?
Many thanks for any assistance/insight that can be provided!
Why do you need so many rigidbodys? Think of it this way: The more objects you have idling, the slower it will be. If you're not doing any collision checking, then why have kinematic rigidbodys at all? (Yes, most of the time there is no difference in performance between very close, non colliding objects and colliding objects)
Thank you for the reply. The reason I have so many rigidbodys is because each can potentially be hit with a bullet, so the bullet needs to have something to collide with and react to.
The buildings/cars scroll past from right to left. At any one time, there are a maximum of maybe 20 in the game. I create them offscreen on the right, move them through the scene to the left, then destroy them when they're offscreen. I might try sleeping them as they are created, and setting a sleepVelocity as their current speed (which remains constant throughout their life). If they're hit by another kinematic rigidbody (i.e. a bullet or the player), that should wake them, correct? Would this help with game performance?
The other thing I have considered doing is not creating/destroying the gameObjects as they're needed - ins$$anonymous$$d I would create everything at the start of the game and leave each object sitting idle until needed, resetting them once they've been used and effectively recycling them. Potentially helpful?
Thanks!
Well definitely avoid instantiating things when the game is playing - that's a killer on performance.
Perhaps its the performance of your collision handling code, could you post some?
You do know, that you do not need a rigidbody
on both sides of the collision? From what I understand you just have a bunch of moving geometry and a bullet. Unless you want that geometry to affect physics (move things), there is no need for rigidbody
s to be attached to it.
Always avoid Instantiating, but you should also use the profiler (in pro) or just the stats popup (in free) to see what is causing your performance drop.
Benproductions: You're right... I didn't realise that I didn't need a rigidbody on both sides of a collision. I got that notion from the Unity doco, which seems to say that if you're planning on moving an object, it needs both a collider and a rigidbody:
A Static Collider is a GameObject that has a Collider but not a Rigidbody. Static Colliders are used for level geometry which always stays at the same place and never moves around. snip
Static Colliders (without a rigidbody) should not be disabled, enabled or transformed. Altering these Static Colliders will cause an internal recomputation in PhysX that is quite expensive and which will result in a big drop in performance.
But after reading and re-reading, I think they're talking about objects under the control of the physics engine, and not objects that you can move around. I guess I got confused because they talk about static colliders right after kinematic rigid bodies (http://docs.unity3d.com/Documentation/Components/comp-DynamicsGroup.html)
So I will try removing the rigidbodies from everything except my bullets and see how that improves things. I will also alter the code to recycle the gameObjects rather than create/destroy.
Answer by briosh · Jan 05, 2014 at 10:32 AM
Very helpful thread - I'd like to add my experience too.
In short: DON'T use rigidbodies on both sides and dont use them even as kinetic in objects that will not behave 'physically' i.e. debris or falling rocks. Keep using them though in projectiles and remember to move them with velocity or forces NOT directly through transform.
The details: Following the unity docs i added one kinetic rigidboy to the root transform of my collidable objects. I use path finding to move around 40-50 enemies at the same time. Since path finding already keeps nav agents from getting inside one to another, the rigidbodies where usefull only for my custom collision checking.
BUT, It turns out that since the bullets are already rigidbodies and the player is a character controller, ALL collision AND trigger events fire correctly WITHOUT the rigidboies and performance also went up from 23fps to 57-70fps even IN the editor!
Answer by ocdy1001 · Jul 18, 2014 at 11:42 AM
well, the amount of rigidbody's could be a problem with mobile devices, but keep in mind that a moving rigidbody is ALWAYS faster then a object that is moving without rigidbody. The engine doesnt expect a not rigidbody-object to move. even if you don't need physics, a object wich is gonna to move should have a rigidbody! if you dont need physics, set it to kinematic. it is faster. this is said by people working at unity.
hope this helps
$$anonymous$$y problem with this is I have a tons of trees in my game most which can be cut down. I have rigidbody's on the ones that can be cut but its killing my performance right now.