- Home /
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:
http://www.youtube.com/watch?v=1-8Zm3PG4y4
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 :)
Answer by venkspower · Apr 30, 2012 at 11:37 AM
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;
}
Dunno why there was problem with my calculations, BUT your idea delete the jitter effects for now, so big Thumbs Up for you :)
The same thing was happening to me! When I parent the camera Gameobject through editor I get the jittering, but when I set the parent through code the jitter goes away, very strange thing indeed! I wonder if there is any difference between these two methods of parenting.
Tip: you need to parent only once in Start function and not in the Update function. Thank you venkspower for your answer!
Answer by Kryptos · Apr 30, 2012 at 11:34 AM
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.
There's a problem with it. The reason I changed Camera from Update/LateUpdate to Fixed Update is because when using Update it jitter in many cases, for example when free falling ( even if I use smoothed camera movement )
Your answer
Follow this Question
Related Questions
Physics + first person camera = jitter? 4 Answers
Unity3d RTS style camera movement is jittery 0 Answers
Jittery Movement - specific mechanics in mind 0 Answers
Lerping orthographic camera size jitters 1 Answer
Particles lag/stutter - choppy movement 3 Answers