Jitter Camera Effect When Parented To Animated Object

Heya, I was trying to fix this problem on my own for some time, but everything I tried failed so maybe someone can help me with solving that problem.

The thing is I have a Character Controller player who is changing its position and rotation in FixedUpdate() function. I have a moving platform ( cube, uniform scale ) and it has one Animation Clip attached which was made in Unity Animation Editor, and the only thing it does is moving the platform. When player stands on this animated moving platform, he is parented to it, so they can move together. Then at last I have Camera which also is calculated and translated + rotated at FixedUpdate() function to remove jitter effects.

And everything works great except when player is parented to this moving platform, when big jitter effect is shown.

Simpler Camera Script:

protected void FixedUpdate()
{
            transformFrom = _cameraTransform.position;
 		transformTo = player.position + player.TransformDirection( 0,4,-8 );
		
		_cameraTransform.position = Vector3.Lerp( transformFrom, transformTo, Time.fixedDeltaTime * cameraMovementDamping );
		
		_cameraTransform.LookAt(player.transform.position);
	}

In my main script camera rotation is also smoothed with Quaternion.Lerp.

Problem Effect:

At the beginning the video is normal speed, then it slows to show you it in slow motion ( plus my Frame Rate of capturing dropped then ).

Thank in you advande for any kind of help :slight_smile:

You don’t have to calculate the position of or rotate the camera. You just have to child it, as the same way you did with the CharacterController. Make sure, you have made that through the code.

var camera : GameObject;
var platform : GameObject;
function Update(){
camera.transform.parent = platform.transform;
}

Do not move the camera inside a FixedUpdate. There is no need for that. The pruprose of a camera is to shoot the scene, not playing physics.

Therefore, it is a good idea to fix the camera position juste before the scene is rendered.

Fortunately Unity provides juste the right event for that OnPreRender (edit: not the best solution, see below):

void OnPreRender()
{
    transformFrom = _cameraTransform.position;
    transformTo = player.position + player.TransformDirection( 0,4,-8 );

    _cameraTransform.position = Vector3.Lerp( transformFrom, transformTo, Time.deltaTime * cameraMovementDamping );

    _cameraTransform.LookAt(player.transform.position);
}

If this doesn’t work (I did not try it myself), you can still update the camera position in a latter phase using LateUpdate:

void LateUpdate()
{
    transformFrom = _cameraTransform.position;
    transformTo = player.position + player.TransformDirection( 0,4,-8 );

    _cameraTransform.position = Vector3.Lerp( transformFrom, transformTo, Time.deltaTime * cameraMovementDamping );

    _cameraTransform.LookAt(player.transform.position);
}

edit: LateUpdate is the method to use. OnPreRender does not change position/rotation but can be used to set/unset effects such as fog.