- Home /
When and when not to use kinematic rigidbody?
I have a VR game with a scene with a few enemies that the player can aggro, and then kill them with a few sword swings. There are no physics interactions (things bouncing off one another), just colliders passing through each other. Currently, everything only has colliders on them, no kinematic rigidbodies. The docs say that if a collider moves around with no rigidbody (a static collider), it has a performance cost, and there can be inaccuracies with raycasts and such.
Apparently the performance cost of moving a static collider was drastically reduced in a 5.x build at some point. This seems true because I made a scene of 1000 moving cube with and without kinematic rigidbodies. When I ran the game without kinematic rigidbodies, I got ~70fps. When I ran the game with kinematic rigidbodies on the cubes, I only got ~13fps, with most of the processing going to the physics (I also used the rigidbody.position to move the cube).
So by those numbers, I feel like if I'm not using a rigidbody to behave with physics, I should just not use it because it adds more processing. But, does not having a kinematic rigidbody on a collider cause inaccuracies still? Is it fine for my sword and enemies to only have colliders and no kinematic rigidbody? Or what is most efficient/accurate way?
It is getting hard to trust what the docs say...
Why do you set the rigidbodies to kinematic? Do you know what kinematic means?
Yes I do know what is$$anonymous$$inematic does. I do not want physics to control the positions of the objects, so if I were to use a rigidbody, I would set is$$anonymous$$inematic to true. I would just not use rigidbodies at all, but what I keep seeing on the internet is that moving a collider with no rigidbody is a no no. I've read through the docs on these multiple times ;).
Well, that is a no no. I just don't understand if you are not going to use physics why use colliders.
Did you take a look at the Unity example project how characters are build? https://unity3d.com/learn/tutorials/s/survival-shooter-tutorial
Answer by Harinezumi · May 03, 2018 at 08:15 AM
Based on your description I think you need is trigger colliders with kinematic rigidbodies. Rigidbodies because they move, trigger because things don't bounce off each other, and kinematic, because they are moved by logic.
Why do I say this? Well, this is how I basically think about physics in Unity. A game object...
- ...without collider: does not take part and/or care about physics; purely logic or visual
- ...with trigger collider: a static (non-moving) detector area, for triggering logic when something enters it
- ...with (non-trigger) collider, without rigidbody: static physical object, real, solid things that won't move, like walls, the ground, etc.
- ...with (non-trigger) collider and non-kinematic rigidbody: dynamic physical object, a thing that moves according to the laws of physics, like boxes, balls, projectiles, etc.
- ...with (non-trigger) collider and kinematic rigidbody: dynamic physical object, but it doesn't behave purely with physics, usually because it is moved by player input or animation or some other logic
These cover most cases and makes it pretty clear what to use in what situation. Note that you can switch kinematic on and off depending on the situation. For example, normally your player character can move with user input with kinematic rigidbody, but when it gets blown up by and explosion you make it non-kinematic and let physics handle how it flies and falls.
Btw, as far as I know these are all the collider types in 3D: SphereCollider, BoxCollider, CapsuleCollider, MeshCollider, AND CharacterController.
This is what I was thinking too, to use trigger colliders with kinematic rigidbodies. However, in my test scene with 1000 moving cubes with trigger colliders, when having kinematic rigidbodies on the cubes, there was much more physics processing going on, resulting in about 13fps. And without rigidbodies, I got around 70fps. So with those results, it makes me think that if hit detection is still accurate, I shouldn't use rigidbodies then since they are more processing intensive to move. Any thoughts on that?
That is surprising, because moving static colliders is supposed to be costlier than ones with rigidbodies! I'm pretty sure that using trigger colliders with kinematic rigidbodies should work fine, but maybe this is a bug, or other quirk of the physics system.
So I researched a bit, but I could not find any information or reported issue about the topic. The only thing I can think of is that maybe rigidbody.$$anonymous$$ovePosition()
should be used (maybe rigidbody.position
(which instantly moves) is costlier? This is just a guess). You could also try to use rigidbody.velocity
, but it feels like a hack...
Sorry, this is all that comes to my $$anonymous$$d. If you do find a solution, please post it, so that others may find as well. Btw, what version of Unity are you using?
Answer by Harley-B · Jul 12, 2018 at 07:11 AM
You did the right thing. You tested it - don't worry about what the conventional wisdom is, worry about results. I've run similar tests with similar results and I move my colliders around without rigidbodys too.