Brief Description: I have a RB based character controller that I want to move along with my Train platform (non physics based) when the player steps on it.
Main Problem I manage to make the player follow the platform, but I also have a camera jitter problem that I can't solve without breaking player follow the Train platform logic.
Video showing the jitter.
Important: I'm not getting any jitter when only moving the camera
Important: I'm not getting any jitter when only moving the Character without moving the camera
this applies to both when the player is on a moving platform, or off the platform
https://www.youtube.com/watch?v=9M4dt00dEes
My Camera Implementation
For handling the camera I'm using Cinemachine, alongside a CameraHolder which is a separate gameobject from the player.
I'm setting the transform of the cameraHolder Gameobject to the players position in late Update
private void LateUpdate()
{
transform.position = cameraPosition.position;
}
The Headbob Offset gameobject is a child in the cameraHolder without any movement.
Player Controller Movement
I'm using this code in FixedUpdate to handle my player movement
private void HandleMovement()
{
//old input system
//float horizontal = Input.GetAxis("Horizontal");
//float vertical = Input.GetAxis("Vertical");
//Vector2 movementDir = new Vector2(horizontal, vertical).normalized;
//New input system
movementDir = GameInput.instance.GetMovementVectorNormalised();
Vector3 currentVelocity = rb.velocity;
Vector3 targetVelocity = new Vector3(movementDir.x, 0, movementDir.y);
targetVelocity *= speed;
targetVelocity = transform.TransformDirection(targetVelocity);
Vector3 velocityChange = targetVelocity - currentVelocity;
velocityChange = new Vector3(velocityChange.x, 0, velocityChange.z);
Vector3.ClampMagnitude(velocityChange, maxForce);
rb.AddForce(velocityChange, ForceMode.VelocityChange);
}
RB interpolation is set to Interpolate
Train Platform Movement Script
private void FixedUpdate()
{
transform.position += transform.forward * currentSpeed * Time.deltaTime;
}
Important I'm using FixedUpdate, but my train platform has no RB on it, I'm using FixedUpdate, because otherwise my player doesn't follow the train position change.
What I've Tried so far
1. Lerping Camera Position
Instead of setting the camera position to the cameraHolder, I tried lerping. It solved the overall jitter in the game, but my whole train platform became jiterry when moving.
private void LateUpdate()
{
transform.position = Vector3.Lerp(transform.position, cameraPosition.position, lerpSpeed * Time.deltaTime);
}
Videos showing the problem
jitter almost solved https://www.youtube.com/watch?v=sxXXk_oVpsg Train jitter https://www.youtube.com/watch?v=85biDT_nl_4
2. Add dumping to Cinemachine virtual camera
results are the same as with lerping. everything else is smooth besides the train
I'll provide any necessary information if I haven't mentioned something here