Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Display shaking when moving camera

Discussion in 'Scripting' started by tribaleur, Apr 20, 2018.

  1. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    Hi guys,

    First of all, i'm not good in english and i'm not sur about "shaking" translation
    By Jerking i mean : it's a bit like if i had 20FPS.

    I'm working on a 3D Isometric game.
    With a static camera, everything is OK. But when i add this little script to my update void in my character Gameobject, my display is "shaking". The camera movement is ok. But i don't understand why this "shake" come.

    Code (CSharp):
    1.  
    2. this._camera.transform.position = new Vector3(this.transform.position.x, this._camera.transform.position.y, this.transform.position.z-10);
    My computer is pretty low. But i have juste a 500 units plan with a Character model that i move. And my camera that i'm trying to moving.

    Here my camera properties :


    I don't know how to show you what my problem looks like.

    Maybe it's not a good idea to move camera like this ?

    Thanks.
     
    Last edited: Apr 20, 2018
  2. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    That script is fine, maybe the one shaking is the player. Jerkin can mean something else :D.
     
  3. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    Thanks for you translation help. I changed my post. ;)
    Should i move my player in fixedupdate ? I use transform.translate in udpatde void.

    Bellow my move void :
    Code (CSharp):
    1. public void move () {
    2.         float translateX = this._currentSpeedX * Time.deltaTime;
    3.         float translateY = this._currentSpeedY * Time.deltaTime;
    4.         float translateZ = this._currentSpeedZ * Time.deltaTime;
    5.  
    6.         if (Math.Abs(translateX) < this._minTravelDistance) { translateX = 0f; }
    7.         if (Math.Abs(translateY) < this._minTravelDistance) { translateY = 0f; }
    8.         if (Math.Abs(translateZ) < this._minTravelDistance) { translateZ = 0f; }
    9.  
    10.         this.transform.Translate(translateX, translateY, translateZ, Space.World);
    11.     }
     
  4. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    You seem to be doing it right, is your player shaking too? Look at him trough the scene view, not the Game view.
     
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,822
    Shaking can be a sign of floating imprecision. What sort of position values is the camera at? Are they high, in the tens of thousands?
     
  6. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    Hi guys and thanks for your answer.

    I checked that and my character is not shaking in scene view. Maybe the walk animation make him lean on left or right on each step ? And this little lean could change my transform.position a bit ? Does animation change transform.position of there rigged GameObject ?

    My Camera is 11 unity on Y axe. And moving around 0-500 on X and Z axes. Rotation doesn't change. Bellow my camera default settings.
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,822
    Doesn't look like floating imprecision. Is your camera attached to the character? The animation could be causing the issues. Try a script to track the player instead with some dampening/lerping to keep it smooth. Cinemachine on the asset store has some nice stuff for this.
     
    whileBreak likes this.
  8. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    My camera is not attached to the character. It has his own script to follow the character.
    Code (CSharp):
    1. this._camera.transform.position = new Vector3(this.transform.position.x, this._camera.transform.position.y, this.transform.position.z-10);
    I don't know what is dampening and lerping. I'm gonna check that and let you know.
    Thanks for your help.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    To the best of my knowledge, if you move a character in Update, you should use LateUpdate for the camera.

    (FixedUpdate moves should move the camera in fixed update).

    Note: This isn't meant to replace the suggestion of smoothing/dampening, etc..
     
    OlSim17, ysavan271 and karl_jones like this.
  10. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    karl_jones likes this.
  11. tribaleur

    tribaleur

    Joined:
    Jan 19, 2017
    Posts:
    34
    So I changed my update void to LateUpdate void. It doesn't look like it change anything but i let this because it's better according to the unity doc.

    I changed my moving camera script as bellow and it's better after few try to find the good lerp speed :
    Code (CSharp):
    1. Vector3 targetPosition = new Vector3(this.transform.position.x, this._camera.transform.position.y, this.transform.position.z) + this._gap;
    2. this._camera.transform.position = Vector3.Lerp(this._camera.transform.position, targetPosition, this._lerpSpeed);
    The other think that it change because i'm using a low lerp speed (0.1f) is that my camera have a cool delay effect when i move my character. Wasn't looking for that but it's pretty nice. :D

    Thanks for your help guys. It look like you solved my problem.

    :)
     
    Last edited: Apr 24, 2018
    whileBreak likes this.
  12. Yrandika

    Yrandika

    Joined:
    Sep 17, 2018
    Posts:
    2
    This is really old post, but I got the same problem lately ,and I fixed my camera shaking problem using LateUpdate and Time.delta time for the camera smooth damping. so my case was
    transform.position = Vector3.SmoothDamp(transform.position, finalPosition, ref refVelocity, camSmooth * Time.deltaTime);
    here Just using camSmooth without the Time.deltaTime was the reason for my camera shaking