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. Dismiss Notice

Smooth camera movement?

Discussion in 'General Graphics' started by Ljaljevic1120, Apr 1, 2020.

  1. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    I'm making a 2D Android game and I ran into an issue with some camera movement.

    I'm trying to smooth out the current camera movement of my game. In the game, there's a ball that bounces left and right. When the user touches the screen, the ball also moves up. The camera follows the ball when it moves up, but it is noticeably jittery.

    The ball uses physics, so I move it with
    Rigidbody2D.MovePosition()
    inside FixedUpdate. The camera is moved by setting its transform. I've tried every combination of Update, LateUpdate, and FixedUpdate. Nothing seems to work...

    Is there any way to move the ball and camera smoothly?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    If your fixed update rate and your game update rate don't match, the movement of that object is going to be a little jittery as, depending on your fixed update rate, some updates the fixed update might get called multiple times, or not at all, within a frame update. For single objects moving this often isn't super obvious. For the entire camera moving that way it most certainly will be. Usually the answer is to use smoothing on you camera, and potentially even the rendered object's position, that is moved in Update rather than in FixedUpdate, but using the FixedUpdate position to inform the movement.

    There are lots of "Smooth Camera" examples out there. Try one of them.
     
  3. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Hello, thanks for the reply!

    I've already tried smoothing the camera with
    Vector3.Lerp()
    . I've tried this in Update, LateUpdate, and FixedUpdate. Still jittery, and I can't find anything similar on the internet.

    This is why I'm confused... Any ideas?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    austinborden and valarus like this.
  5. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Oh, I did not know that. Thanks!
    I'll try it now...
     
  6. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Okay, I've just changed around my code and got it to work.

    I tried putting it in Update, LateUpdate, and FixedUpdate. Update appears to have the smoothest look.
    Does it matter where I put it?
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    Update or LateUpdate should both look the same as long as nothing else is moving it. You don't want it in FixedUpdate.
     
  8. Ljaljevic1120

    Ljaljevic1120

    Joined:
    Jul 15, 2019
    Posts:
    85
    Perfect! Camera is following ball as smoothly as I can get it.

    Thanks for your help :)
     
  9. animatedpickletoo

    animatedpickletoo

    Joined:
    Sep 1, 2020
    Posts:
    1
    This was an epic thread
     
  10. austinborden

    austinborden

    Joined:
    Aug 5, 2016
    Posts:
    24
    Always heard about Lerp, never about SmoothDamp. This is definitely better, thanks!