New Camera, New Attack System, and New Problems!
An article that touches on a couple new systems in my space faring Metroidvania game and the problems that arose!
11/21/20243 min read
Fix 1 thing, and another breaks elsewhere...
I've updated a lot of things the past few weeks! Most notably, the player aim/attack system, and the camera system!
The Camera system was updated to more effectively prevent camera clipping (when the camera is able to go through walls allowing the player to see the world sky box and/or other sections of the level). I was able to build a system from scratch that did a great job preventing the player from moving the camera out of bounds, or rotating the through a wall when turning the camera. However, it failed when the player was moving and rotating the camera at the same time.
Enter Cinemachine!
Cinemachine is a virtual camera solution that Unity purchased for their developers. I heard about Cinemachine but it wasn't until a couple weeks ago that I actually started to research and learn how it could benefit my game.
The benefits allowed me to:
very effectively prevent camera clipping.
provide the user with better camera control.
Allowed me to introduce an over the should aiming system for our projectile attacks.
and I haven't implemented it yet, but I believe this is going to make cutscenes a breeze!
The cons of implementing Cinemachine... :
Many things had to be updated as they were no longer working as they were supposed to.
The main problem that I encountered was Jittery movement while on moving platforms. It didn't matter what I did... The camera was jittery, my player movement was jittery, and the moving platforms were stuttering across their paths!
I can laugh about it now... but the most frustrating part was when I thought I found a solution as the player was finally moving and rotating smoothly on the moving platforms... but as soon as I hopped off and traversed the world on normal ground, the movement was stuttering again.
My Solution:
It got to a point where I was debating scrapping my player controller script and starting fresh. I took the night off and came back and decided to try something that never ocurred to me to try. I changed the Cinemachine brain update setting to "fixed update" (this was the solution that made the player move smoothly on moving platforms, but not on normal non moving platforms), and I then went to my players rigidbody and did the opposite of what every rigidbody tutorial tells you to do. I switched the interpolate setting to "None".
Low and behold, smooth movement everywhere!
Cinemachine Brain Update fix:
Unity's FixedUpdate is designed to synchronize with the physics system, which also runs at a fixed timestep. By default, Cinemachine uses LateUpdate for its updates, which runs every frame, potentially at a different rate than the physics updates.
When the player is parented to a moving platform, the platform's position updates in FixedUpdate (physics timestep), but the camera updates in LateUpdate (rendering timestep). This can create jitter because:
The player's position changes slightly during each physics update.
The camera lags behind, interpolating or extrapolating movement based on outdated data.
Switching the Cinemachine Brain to FixedUpdate ensures that the camera updates in sync with the physics engine. This reduces the desynchronization that causes jitter when the player is on a moving platform.
Rigidbody interpoloation to "None"
Rigidbody interpolation smooths motion by interpolating the object's position between physics updates to match the rendering framerate. While interpolation is useful for objects observed by a camera, it can cause issues when combined with a camera system that also smooths movement, like Cinemachine.
Here’s why:
With interpolation enabled, the Rigidbody slightly offsets its visual position compared to its true physics position to make motion smoother.
Cinemachine attempts to follow this smoothed motion, but since the Rigidbody isn't at its "true" position, it introduces subtle inconsistencies.
By setting interpolation to None, the Rigidbody now updates directly based on the physics calculations, removing this extra smoothing layer. Since the camera is now also updated in FixedUpdate, it synchronizes perfectly with the player's true physics position.
Why These Fixes Worked Together
FixedUpdate for Cinemachine: Ensures the camera follows the player in lockstep with the physics system, avoiding mismatches during moving platform movement.
No Interpolation on Rigidbody: Removes redundant smoothing that conflicted with the camera’s smoothing behavior, especially when the player moved independently.
I wasn't able to find a fix for my problems anywhere so I hope this article is able to help someone facing a similar problem down the line!
Until next time!