0
\$\begingroup\$

I'm learning to make a Tilebased game with Unity and for that I use the Zelda alttp assets. I just have Tiles, a Camera and a moving Character with a rigidbody2D and a CircleCollider.

When the Camera follows the Character, the scene feels Jerky.

Info:

  • The Camera uses the 2D Pixel Perfect package.
  • The Character is moving via Rigidbody2D.MovePosition() in the FixedUpdate Method:

code:

void Update()
{
    float yInput;
    float xInput;

    #region yMovement
    if (Input.GetKey(keyMoveTop))
    {
        if (Input.GetKey(keyMoveBot))
        {
            yInput = 0f;
        }
        else yInput = 1f;
    }
    else if (Input.GetKey(keyMoveBot))
    {
        yInput = -1f;
    }
    else yInput = 0f;
    #endregion

    #region xMovement
    if (Input.GetKey(keyMoveLeft))
    {
        if (Input.GetKey(keyMoveRight))
        {
            xInput = 0f;
        }
        else xInput = -1f;
    }
    else if (Input.GetKey(keyMoveRight))
    {
        xInput = 1f;
    }
    else xInput = 0f;
    #endregion

    //Walking
    isWalking = Input.GetKey(keyMoveWalk);
    moveDirection = Vector2.ClampMagnitude(transform.right * xInput + transform.up * yInput, 1f);
}

private void FixedUpdate()
{
    _rb.MovePosition(_rb.position + moveDirection * (isWalking ? walkSpeed : runSpeed) * Time.fixedDeltaTime);
}

You can see the problem here: https://youtu.be/MA2zZPME5X4

Edit: Added all my movement code

\$\endgroup\$
5
  • \$\begingroup\$ Have you looked at your framerate? Is it 60 fps (or whatever the refresh rate of your monitor is)? \$\endgroup\$
    – Foxwarrior
    Jan 17 '20 at 19:11
  • \$\begingroup\$ No I didn't but it would be very weird to not have max fps plying this "game" since it's a gaming computer ^^ \$\endgroup\$
    – Sharpnel
    Jan 17 '20 at 21:18
  • \$\begingroup\$ @Foxwarrior Unity shows I have 70 to 80 fps during playmode \$\endgroup\$
    – Sharpnel
    Jan 17 '20 at 22:07
  • \$\begingroup\$ Could it be a rounding problem in your movement code? Like if walkspeed was 0.6 pixels per frame, then it would appear to move 0,1,0,1,1,0,1,0,1,1 pixels in a given frame, and look very jerky. Or maybe it's because you're moving in FixedUpdate but rendering in Update, which aren't synchronized with each other. \$\endgroup\$
    – Foxwarrior
    Jan 17 '20 at 22:21
  • \$\begingroup\$ @Foxwarrior I removed the PixelPerfect component to the Camera (so I think there's nothing that rounds values now) the problem is still here :( \$\endgroup\$
    – Sharpnel
    Jan 17 '20 at 22:57
1
\$\begingroup\$

Cameras are very sensible to anything that does not move them perfectly smoothly. The camera should be updated in an Update or LateUpdate. One way to handle cameras is to adjust variables in a FixedUpdate, and to move the camera in the (Late)Update based on the given variables. Moving the player in a FixedUpdate with the camera being parented to will cause flickering.

The reason why is because FixedUpdate does not align with the frame rate. Update and LateUpdate however do, which is why these are the ideal moments to move your camera.

Many people like to attach their cameras to the player, but I'd discourage from doing so. Cameras should have their own, controlled moving patterns unrelated to the arbitrary movements of the player, otherwise you risk all sorts of flickering issues like this including a loss of camera quality in general.

\$\endgroup\$
9
  • \$\begingroup\$ Things I just tried: I tried using a virtual Camera (with Cinemachine) that follows the player with some damping, same problem. I tried moving the Character in the Update Method, same problem. I even tried just moving the camera but it does the same thing. Maybe is a problem with the code? (I used the same code with all my attemps) I'm lost :( \$\endgroup\$
    – Sharpnel
    Jan 17 '20 at 22:52
  • \$\begingroup\$ @Sharpnel - Alright, then it sounds like a performance issue. Maybe you wrote some horrible code. Often a combination of instantiates, Debug.Log and wild loops. Look into: Unity Editor > Window > Analysis > Profiles (or Ctrl+7). See if there are weird spikes during runtime. If you can't figure it out, activate "Deep Profile." That may slow down everything, but you'll get pinpointed to the exact code part which causes performance issues and also some neat statistics. Alternative tool is to use System.Diagnostics.Stopwatch and test suspicious code parts. \$\endgroup\$
    – Battle
    Jan 17 '20 at 23:10
  • \$\begingroup\$ I've created a new scene and added to it a sprite. I attached the Movement code to the camera (I added a rigidbody2D to the camera) and that's it. The problem is still here ^^ (I have edited this topic and added all my Movement code) \$\endgroup\$
    – Sharpnel
    Jan 17 '20 at 23:14
  • \$\begingroup\$ (weird?) spikes: imgur.com/a/0HLr76N \$\endgroup\$
    – Sharpnel
    Jan 17 '20 at 23:23
  • 1
    \$\begingroup\$ @Sharpnel - It's ok, somebody else might stumble upon this who might have issues with their cameras flickering and will find some answers. \$\endgroup\$
    – Battle
    Jan 19 '20 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.