I noticed a strange 4-20 second delay happening in my game and I narrowed it down to the following
2 lines of code that set the transform.position of a gameObject. In my last profile capture, each of these took 2 seconds, but it had taken even longer in previous tests.
Does anyone know what could explain this? Obviously the first set of code was setting the transform position twice when only once was necessary, but why would the first code cause a massive profile spike?
Note the object in question is an object, with a few child objects, and those child objects have hundreds of colliders. I’m sure it’s related to these colliders, but still seems like a mystery.
Sounds like there is more going on here than just the two settings occuring… are these in an update/fixedupdate loop? If so, does posDelta change? Does attachedWand move?
Oh okay, so this a VR project then. Well, perhaps if there really is something strange going on that isn’t caused by your own code, somebody else will have ran into it as well, and have an answer for you. Sorry but I haven’t had a chance to get into VR dev much yet, so I can’t be of much assistance!
Sorry, this isn’t one that I can create a reproduction case for. I think it’s an extreme edge case, and my project is far too large to send. thanks, though. I did find a workaround, so (even for me) it’s a low priority. Really I was wondering if anyone had an idea about what might cause this issue.
You mention that the object has children with lots of Colliders. This suggests to me that they are marked as static or do not have RigidBody components attached. This means that PhysX will recalculate all these colliders when they are moved.
If you look in the Profiler it should show up as StaticCollide.Move (Expensive delayed cost) or something similar.
If I remember correctly Unity were talking about removing the restriction on this if there is no RigidBody attached but it is also not marked as static. I’ve always just added a Kinematic RigidBody to every Collider that isn’t static that might move at some point anyway to make doubly sure.
TL;DR Add a RigidBody to each GameObject that has a Collider on it that moves.
thanks, will check this. Regarding adding RigidBodies, isn’t it the case that when you have a parent object with children, and you set the parent to non-kinematic, that you should remove the Rigidbodies from the child objects? My understanding is that when you do this, the parent Rigidbody takes over and all the sub-colliders are handled by it.
That is correct, but from what I understand, moving the child colliders within the RigidBody will cause the same re-calculation to occur. So you can group up your colliders under a parent RigidBody, as long as you don’t then move the children independently from the parent.