- Home /
Moving Colliders giving me Physics.simulate spikes in Profiler
Hi there,
i'm currently prototyping a mobile game and i'm a bit stuck because of some performance issues. The thing is: I have a somehow "grid" of cubes (16x16), which are moving every now and then (usually each 5 seconds). They all need to have a collider/Trigger on, because i need to Raycast against each one of them.
Problem is: If i'm moving them by script (setting transform.position), i get ugly lags, caused by Physics.simulate (so says profiler). If i remove the collider everything is smooth, but i really need to Raycast against them... I don't need physics for anything else than the Raycasts, so no rigidbodys or whatever involved. Already tried to increase fixed Time, lowered physics solver iteraton count and all the other Physics settings - with no success.
Any Ideas how to solve this?
Answer by liszto · Feb 17, 2013 at 12:38 AM
If I remember of Physics rules :
If you move object with collider by script it can generate high lags cause an object with a collider must stay static.
If you want move the object by script you must give it a rigidbody an define it as kinematic. Then if you want move it during an update, you must move it in fixedUpdate and not in Update.
As advice, yo ucan watch the video named "Unite 2012 - Performance Optimization Tips and Tricks for Unity" at this link : http://unity3d.com/unite/archive/2012 and watch between 21.30 and 25.00 roughly. All your answers are here ;)
Thanks a lot for your answer, i watched the video and got a few more hints how to optimize my game, thanks! Actually adding a rigidbody is not really the solution to my problem because i would have a lot of rigidbodys - i tried it and performance is dropping really bad. I do not move the objects in Update (or fixed Update), just every 5 Seconds and as i already said i really don't need them for enything else than raycasting... I'll have to try something else i think.
Think to look Bunny solution I totally forget to talk to you about Physics collision matrix in Edit/Project settings
Answer by Bunny83 · Feb 20, 2013 at 10:23 AM
If you need the colliders just as raycast hit object do this:
Make sure they are set to isTrigger
Create a new layer for the objects and put them all on this layer
Go to Edit -> project settings -> Physics and disable all interactions in the collision matrix. So just clear the row and column of your layer in the matrix.
When raycasting against them make sure you use the layermask and pass a layermask that only contains the layers you want to raycast against.
This little tidbit has just saved me a good 30FPS on a few thousand objects with the game time at x100 - it's made it possible to massively expand the number of objects I can include. Thank you!
Your answer
Follow this Question
Related Questions
Mesh collider cost 1 Answer
Fastest primitive collider 1 Answer
Spikes with Physics.Simulate() in empty scene? 0 Answers
Random Performance Spikes due to Physics.Processing in a static scene. 1 Answer
Box collider vs Quad(mesh) collider 1 Answer