Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Smooth following to "spaceship" - jittering

Discussion in 'Scripting' started by smoketh, Sep 15, 2012.

  1. smoketh

    smoketh

    Joined:
    Sep 14, 2012
    Posts:
    29
    First off - i know this topic already been touched upon, however every solution i found not helping - camera still jitters like crazy
    Code (csharp):
    1. var flyingSpeed = 100;
    2.  
    3. var minThrust = 20;
    4.  
    5. var maxThrust = 300;
    6.  
    7. var Accel = maxThrust / 4;
    8.  
    9. //var Jumpspeed = maxThrust *5;
    10.  
    11. var turnSpeed = 100;
    12.  
    13. var afterBurner = 0;
    14.  
    15. var setspeed = 0;
    16. var outputspeed = 0;
    17. var controlTexture : Texture2D;
    18.  
    19. var distance = 12.0;
    20. var height = 5.0;
    21. var damping = 5.0;
    22. var smoothRotation = true;
    23. var rotationDamping = 10.0;
    24.  
    25. function Update() {
    26.  
    27.     if(Input.GetButton("Jump")){  
    28.     afterBurner=1;
    29.     }
    30.  
    31.     if(!Input.GetButton("Jump")){
    32.     afterBurner=0;
    33.     }
    34.     outputspeed = Time.deltaTime * (setspeed+4000*afterBurner);
    35.    rigidbody.velocity = transform.forward * outputspeed; //Apply velocity in Update()
    36. rigidbody.SetMaxAngularVelocity(0f); //killing of any obtained rigidbody.angularvelocity
    37.  
    38.    if(Input.GetAxis("Mouse X") > 0.0){ //if the right arrow is pressed
    39.       transform.Rotate(0.0, turnSpeed * Time.deltaTime, 0.0 * Time.deltaTime, Space.Self); //and then turn the plane
    40.         //rigidbody.AddTorque(0.0,turnSpeed*Time.deltaTime,0.0);
    41.    }
    42.  
    43.    if(Input.GetAxis("Mouse X") < 0.0){ //if the left arrow is pressed
    44.       transform.Rotate(0.0, -turnSpeed * Time.deltaTime, 0.0 * Time.deltaTime, Space.Self);
    45.    }
    46.  
    47.    if(Input.GetAxis("Mouse Y") < 0.0){ //if the up arrow is pressed
    48.       transform.Rotate(-turnSpeed * Time.deltaTime, 0.0, 0.0);
    49.    }
    50.  
    51.    if(Input.GetAxis("Mouse Y") > 0.0){ //if the down arrow is      
    52.       transform.Rotate(turnSpeed * Time.deltaTime, 0.0, 0.0);
    53.      }
    54.  
    55.     if(Input.GetAxis("Roll") > 0.0){
    56.         transform.Rotate(0.0, 0.0, -turnSpeed * Time.deltaTime, Space.Self);
    57.     }
    58.  
    59.     if(Input.GetAxis("Roll") < 0.0){
    60.         transform.Rotate(0.0, 0.0, turnSpeed * Time.deltaTime, Space.Self);
    61.     }
    62.     if(Input.GetAxis("Power") > 0.0  setspeed < 2000){
    63.         setspeed=setspeed+50;
    64.     }
    65.         if(Input.GetAxis("Power") < 0.0  setspeed > 0){
    66.         setspeed=setspeed-50;
    67.     }
    68. }
    69.  
    70. function OnGUI () {
    71. var desiredscale : float = setspeed;
    72. var resultation : int = (desiredscale/2000*70);
    73. GUI.Box (new Rect (0,Screen.height - 80,100,80), ""+desiredscale);
    74. GUI.DrawTexture (Rect (10, Screen.height - resultation-5, 25, resultation), controlTexture);//, Rect (0,0,100,100));
    75.  
    76. }
    77.  
    78. function LateUpdate () {
    79.     var dt : float = Time.smoothDeltaTime;
    80.    
    81.     var wantedPosition = transform.TransformPoint(0, height, -distance);
    82.     var finalizedPos = wantedPosition+transform.forward * outputspeed;
    83.     Camera.main.transform.position = Vector3.Lerp (Camera.main.transform.position, wantedPosition, dt * damping);
    84.  
    85.     if (smoothRotation) {
    86.         var wantedRotation = Quaternion.LookRotation(transform.position - Camera.main.transform.position, transform.up);
    87.         Camera.main.transform.rotation = Quaternion.Slerp (Camera.main.transform.rotation, wantedRotation, dt * rotationDamping);
    88.     }
    89.    
    90.     else Camera.main.transform.LookAt (transform, transform.up);
    91. }
    This stinker makes camera jitter while moving forward.
    Here is an example - and yes - this is not ship jittering but camera. move vessel point-blank to ground and try moving along it - controls:
    Arrows, xc for roll, a/z - acceleration/deceleration, f - afterburner.
    Demo
     
  2. reddotgames

    reddotgames

    Joined:
    Apr 5, 2011
    Posts:
    705
    try changing LateUpdate() into FixedUpdate()
     
  3. smoketh

    smoketh

    Joined:
    Sep 14, 2012
    Posts:
    29
    No effect. Still jitters.
     
  4. smoketh

    smoketh

    Joined:
    Sep 14, 2012
    Posts:
    29
    Oh wait, no. All works now. Now other problem - it jitters a little while rotating:
    Reload demo - you'll see.
     
    Last edited: Sep 15, 2012
  5. reddotgames

    reddotgames

    Joined:
    Apr 5, 2011
    Posts:
    705
    make fixedupdate step smaller. but this will hit performance, so you can use it as temporary solution.

    do you really need use rigidbodies for this? because rigidbodies moves inside physics loop and camera works the best in normal (lateupdate) loop.
     
  6. smoketh

    smoketh

    Joined:
    Sep 14, 2012
    Posts:
    29
    Actually - i just fixed it. I moved entire cycle to fixed update and replaced Time.deltaTime with Time.fixedDeltaTime
     
  7. reddotgames

    reddotgames

    Joined:
    Apr 5, 2011
    Posts:
    705
    right - didnt see that you move physic body in update yay