Search Unity

Question Changing an objects local position by throwing Time.time in Mathf.Sin provides jittery movement?

Discussion in '2D' started by YWainczak, Aug 28, 2020.

  1. YWainczak

    YWainczak

    Joined:
    Oct 1, 2014
    Posts:
    2
    Hello!

    I've been trying to debug a really weird issue. I have a few objects that I'm moving up and down with a sin wave, and while their movement is smooth in the scene view, the game view has their movement appearing really jittery.

    I've gone from moving them by generating a new Vector3 every frame, to having two vectors they lerp between, and I've seen no results. What confuses me is this is a very basic implementation, and I'm not entirely sure why this specific case isn't working, especially since I've done almost this exact same method in UI and it works fine.

    Code (CSharp):
    1. private void Update()
    2. {
    3.     float sin = (Mathf.Sin(Time.unscaledTime * cloudFreq) + 1f) / 2f;
    4.  
    5.     foregroundClouds.localPosition = Vector3.Lerp(cloudDefaultPosition, cloudOffsetPosition, sin);
    6.     backgroundClouds.localPosition = Vector3.Lerp(cloudOffsetPosition, cloudDefaultPosition, sin);
    7.     timeText.text = Time.unscaledTime.ToString();
    8. }
    Time appears to be updating correctly (In the video, I've put it in the UI as the red text on the top left), and originally, I had the objects' parent scale up to fill the screen. I thought this was causing floating point errors, since their original offset was a tiny decimal and I'm using floats, but even after scaling everything up, and having the actual movement increase to about 0.25 total, the movement is still as jittery as it was before.

    Here's a video of it in editor:


    Please let me know if you need any more information, any help would be greatly appreciated!
     
  2. darkhorse95

    darkhorse95

    Joined:
    Aug 11, 2019
    Posts:
    3
    YukonW did you solve this issue ? I am having similar jitter problems.
     
  3. darkhorse95

    darkhorse95

    Joined:
    Aug 11, 2019
    Posts:
    3
    My "Jitter Problem" came down a lot, just by calling the function in FixedUpdate() instead of Update. This might help you too.