Hello,
I have a very simple game and a character that can move in the Level. I also have a level Editor, allowing the users to build their own maps and store them as a file and a scene to load the level file and Instantiate
all the objects in that file.
This works great, as the levels are only around 4 KB in size if the level is very big, but somehow, the movement of the character stutters along with all other objects that are being moved by a script.
They are moved in FixedUpdate
and in all the other levels of the game they move very smooth but in those loaded levels everything is just choppy and jittering.
On PC it’s more of a vibration but on mobile it’s an absolute mess. Could this be caused by all those objects that get instantiated on play or something different? To clarify again, in the levels that are included in the build I have no stutter at all.
You have 2, rather common, problems here:
1. Your camera rig produces frame stutter
This must be a constant issue but noticeable when fps drops below 60 only. High PC framerate just hides it.
Procedure to fix this is different depending on your camera rig specifics but most common cause for this is when a Camera
follows (or is parented to) a RigidBody
. Stutter happens because rendering update rate ( fps & Update
calls) differs from simulation update rate (physics & FixedUpdate
calls) but your camera requires both to be synchronized.
2. Runtime mesh instantiation performs no static optimizations.
Runtime mesh instantiation performs no static optimizations automatically because these are costly to perform and only a programmer knows when to execute these mid-game. This is why FPS drops - because no static mesh optimizations means more (render) Batches
.
To fix this, parent all your newly instantiated level meshes under a single transform and call StaticBatchingUtility.Combine(parent)
function once.
Unity - Scripting API: StaticBatchingUtility.Combine
Thank you for your answer! I tried those hints and they dont seem to bei working. I have claryfied what I wanted to say by “all other objects”, which is all other objects that are being moved by a script. Everything else is smooth and I use Cinemashine for the camera so it follows automatically.
Alright, after a lot of testing I found out that using just the Update
function and multiplying by Time.deltaTime
gave the smoothest result, but only if you use the transform.position
instead of rb.position
(I have no idea why)