Search Unity

Stuttering camera at high speeds

Discussion in 'Scripting' started by phoen, Jan 5, 2007.

  1. phoen

    phoen

    Joined:
    Oct 25, 2005
    Posts:
    94
    Currently Im working on a space-shooter-demolevel.
    The ship rotates through mouse axis and i have a camera following it (smoothed with Lerp).

    My problem:
    The game is played at very high speed. My ships animation looks unsteady - strangely everything runs smooth when i have low framerates.

    I tried several things. What worked best so far is putting both (movement of camera and ship) into FixedUpdate. The animation looks smooth now. The environment however gets stuttering (a little) when framerates change.
    ..and i get the following error:

    !CompareApproximately (SqrMagnitude (m_LocalRotation), 1.0F)
    probably caused by Quaternion.Slerp.
    Quaternion.Lerp works without error.

    p.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    I presume you are using rigidbodies?

    You should do your camera updating in LateUpdate and your player updating with physics in FixedUpdate. Then you should turn on rigidbody interpolation for the players rigidbody.

    This way your rigidbody will be interpolated between the physics frames and the camera will follow the interpolated position in a smooth way.

    http://unity3d.com/Documentation/ScriptReference/Rigidbody-interpolation.html
     
  3. phoen

    phoen

    Joined:
    Oct 25, 2005
    Posts:
    94
    Yes rigidbodies with interpolation turned on.

    When the camera is in LateUpdate the animation looks very jittery.(framefate dependent - it gets worse when i fire weapon)
    I also get the !CompareApproximately error again. (is this something i can fix?)
     
  4. phoen

    phoen

    Joined:
    Oct 25, 2005
    Posts:
    94
    What if several scripts have LateUpdate functions?
    Which one is executed first?
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    It's undefined and you shouldnt rely on one or the other coming first.
    Instead you probably want to move one of them into the Update function, or alternatively call one function from the other to make the execution order explicit.
     
  6. ScatterBrain

    ScatterBrain

    Joined:
    Sep 23, 2006
    Posts:
    37
    I ran into somethng familiar, perhaps you are seeing the same thing.

    What could be happening is that you are basing your camera's up direction on your ship's transform. So when the camera is lazily making its way to where it should be, the camera's up position is getting confused.
    If you watch it step by step, you will see the camera transform SNAP 180 degrees on it's forward axis.

    This is of course if this is the problem. Hopefully I gave you some insight.
     
  7. phoen

    phoen

    Joined:
    Oct 25, 2005
    Posts:
    94
    I think its really because the cameras LateUpdate is called after other LateUpdate functions. Now i use two cameras, one for the player(fixedupdate) and one for the background(lateupdate). It works, i hope it doesnt take away too much performance.
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Using two cameras -if you turn off clearing- doesn't have much overhead.
    If it works, fine, but i would still try to make sure the execution order is right. By either moving player movement code to Update instead of LateUpdate or calling the Update functions which depend on each other yourself, so you can fully determine the execution order.