Search Unity

Unity Camera Stutter With a Basic Camera

Discussion in 'Windows' started by polymoongames, Aug 20, 2017.

  1. polymoongames

    polymoongames

    Joined:
    Aug 17, 2017
    Posts:
    6
    I am having a problem recently. I was making a 2d project I realized i was having some stutter on my camera so i decided to make a simple scene so i could see if my problem is about anything or with my camera. I have only 1 camera 1 moving object on x direction and a tiled background to see if the camera stutters. I have tried with both unity 5.2 and 2017.1 but no luck. The results are the same. The camera is stutering. vsync was affectting me a lot but even turning it off helped a bit but decreasing fixed time made it sooooo smooth. http://www.skgames.net/wp-content/uploads/2013/10/Unity_4_2_2_Stuttering.mov (This is not my project but the problem is same) In my scene there is only 1 main camera 1 moving object on x and a tiled background. (Also a thread opened here with same issue in 2011 here is the link : https://forum.unity3d.com/threads/stuttering-issue-on-unity-4-2.203552/ ) I have been trying to figure this out for 5 days but :/ I believe my codes are not the problem since the code is only 1-2 line but if you wonder here it is :
    (This is for the basic project only camera following a moving object on x direction with a tiled background (in order to see if the camera stutters or not))


    Public class MovingObjectScript : MonoBehaviour {

    public static MovingObjectScript instance;

    [SerializeField]
    private Rigidbody2D myRigidBody;

    [SerializeField]
    private Animator anim;

    private float forwardSpeed = 2f;

    void Start()
    {
    SetCameraX();
    }

    void FixedUpdate ()
    {
    Vector3 temp = transform.position;
    temp.x += forwardSpeed * Time.deltaTime;
    transform.position = temp;
    }

    void SetCameraX()
    {
    CameraScript.offsetX = (Camera.main.transform.position.x - transform.position.x) - 1f;
    }

    public float GetPositionX()
    {
    return transform.position.x;
    }

    This is for the moving object.

    And this is for the camera

    public class CameraScript : MonoBehaviour
    {
    public static float offsetX;

    void Update ()
    {
    MoveTheCamera();
    }

    void MoveTheCamera()
    {
    Vector3 temp = transform.position;
    temp.x = MovingObjectScript.instance.GetPositionX() + offsetX;
    transform.position = temp;
    }
    }

    Besides this I have tried to get the old position in the update and carrying to the new position in late update it gives a slightly better smoothness. Using fixed time with a value of 0.2 makes it horrible (The stutter is so obvious when its 0.2). Using only update with 0.2 still bad. I am about eat my brain because of this error. I want to solve this permanently without changing fixed steps. If someone can help me solving this problem. I will pray for him/her :) (All this made with without vsync. When I turn it on it goes really bad and even decreasing timesteps dont help it)
     
    Last edited: Aug 20, 2017
  2. polymoongames

    polymoongames

    Joined:
    Aug 17, 2017
    Posts:
    6
    I meant 0.02
     
  3. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,900
    Don't move the camera in FixedUpdate(). It will not be in sync with frame rate and will thus appear as stuttering.