Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Basic 2D Movement: sprite jitters

Discussion in '2D' started by DeadZombieee, Dec 31, 2019.

Thread Status:
Not open for further replies.
  1. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    Hello, everyone!
    I'm kind a new to Unity and having some experience in GameMaker Studio 2 I'm frustrating because of "jittering" problem I've faced with.

    I've watched several basic movement tutorials and tried different approaches, applied pixel perfect package settings according to article at unity.com, but didn't move in an inch. Please save my 2019 year!

    The problem is that my character jitters when moving as if his movement is not smooth:


    My PPC and Sprite settings:
    Screenshot_1.png
    Screenshot_2.png

    Movement code:

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.  
    4.     public float speed = 5f;
    5.     private Rigidbody2D body2d;
    6.     private Vector3 change;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         Application.targetFrameRate = 60;
    12.         body2d = GetComponent<Rigidbody2D>();      
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         change = Vector3.zero;
    19.         change.x = Input.GetAxisRaw("Horizontal");
    20.         change.y = Input.GetAxisRaw("Vertical");
    21.         if (change != Vector3.zero)
    22.         {
    23.             MoveCharacter();
    24.         }
    25.     }
    26.  
    27.     void MoveCharacter()
    28.     {
    29.         body2d.MovePosition(transform.position + change * speed * Time.deltaTime);
    30.     }
    31. }
     
  2. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    Example with animated sprite:
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    As per the docs, MovePosition should be called in FixedUpdate. Turn on physics interpolation. Also don't set the target framerate to 60; 120 and 144Hz monitors are increasingly common. Use vsync.

    --Eric
     
    DeadZombieee likes this.
  4. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    I've tried everything you told already. Nothing helped (
     
  5. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    Here is what I've tried so far:
    1. Different interpolation settings for rigidbody2D;
    2. Used FixedUpdate() instead Update();
    3. Used fixedDeltaTime instead deltaTime;
    4. Different Physics timestep settings;
    5. Different Application.targetFrameRate values;
    6. vsync off/on in Unity settings/NVidia settings;
    7. Used Transform.position instead MovePosition;
    8. Applied PixelPerfectCamera.

    Nothing worked :(

    I've downloaded a couple of open projects from github and from tutorials on youtube, opened them and found they have the same jittering problem whether with player movement or camera movement.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The first video has a bit of jittering caused by some programming problem somewhere, but the second video looks like your monitor just has a poor response time, which can't be fixed with software. That's also evident in the first video.

    --Eric
     
    DeadZombieee likes this.
  7. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    MovePosition does not use interpolation, it moves to a position checking for collisions along the way nothing else.

    Never used MovePosition to move a character around myself. I use velocity using interpolation to smooth it all out. If I leave it to none I get stuttering/jittering exactly like the video. MovePosition seems more like a "warp to" than a regular movement thing. Velocity or AddForce for regular movement.

    @Eric5h5 How the hell can you tell from the video about the refresh rate?
     
    DeadZombieee likes this.
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I didn't say anything about refresh rate, I specifically said the response time. It's pretty obvious, especially if you slow down the playback rate. If it was captured directly from the computer there would be no way to see that, but it's filmed with (I assume) a phone.

    --Eric
     
    DeadZombieee likes this.
  9. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    That's what I meant response time. I guess my eyes are not good for that. Roommate can see it too but he can feel a 6ms delay in wireless game controllers and midi controllers.
     
    DeadZombieee likes this.
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It seems clear to me:

    ghosting.png

    Unity's not doing that on its own. ;) You could add a ghosting effect like that, but it would take a deliberate effort. Otherwise it's just the screen being slow.

    --Eric
     
    DeadZombieee likes this.
  11. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    This is not true. MovePosition queues a command such that when the simulation steps the Rigidbody position's velocity is set so that it moves to the position during the simulation step. This doesn't alter the velocity property you normally read. It is completely compatible with body interpolation and is nothing more than a convenience method for something that you could do yourself by manipulating velocity. Note we also temporarily disable gravity/drag so it can get to the target position but this is all hidden during the simulation step.

    When you issue the command, nothing happens. During the simulation step the body has the appropriate velocity so moves through space as expected.

    The only thing that isn't compatible with interpolation is directly modifying the body position or modifying the Transform.
     
  12. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    This is good to know., thank you for clarifying that

    So what could be causing the stuttering the OP is experiencing?
     
    DeadZombieee likes this.
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    Impossible to tell. If it were me I'd check out the profiler to make sure nothing crazy is happening when running in the editor if the "jittering" is really bad. Beyond that, it's being run in the editor so the main-thread isn't just about the game script, it's doing "editor stuff" too so produce a build and see if it's there. Note that spinning up the profiler means the editor is doing more so can make the "jittering" worse. This is why you need to isolate it in a build. You can also turn-on vsync to ensure you're not seeing issues with that. With it on however can mean if you're doing too much stuff per-frame then your framerate will take a serious hit which also shows as "jitter".

    You can also do a basic test with a new project, a single GameObject with a SpriteRenderer only (no physics or other scripts) by just modifying the Transform position per-frame. Maybe get it to move back/forth across the screen.

    As has been said above, that blur will be the monitor. Unity (or any rendering set-up) has no way to control that and no bug would cause it. Blur is an artifact and you have to go out of your way to produce it via a shader etc. This is what response time indicates on a monitor. For the test project I mentioned previously, you could expose a speed property and see if the "blur" gets worse the faster it goes (which it will) and better when moving slowly. Also, note that to really test this, use a high-constrast sprite against the background so white sprite on black background; this will really show the rise/fall time (response) of the monitor.
     
  14. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Thank you @MelvMay this is great info. I hope the OP can use it to solve their problems.
     
    DeadZombieee and MelvMay like this.
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    I know you've indicated you've tried this but the code you posted (as has been mentioned) is wrong. You're issuing MovePosition per-frame when that command isn't even actioned until the simulation steps (per fixed-update). You're also referenceing the Transform.position (for some reason) rather than Rigidbody2D.position. It should be clear that Rigidbody interpolation is the act of adjusting the Transform per-frame from the previous Rigidbody position to the current Rigidbody position. This means that if you're continually (per-frame) referencing where to move relative to the current interpolated Transform position you're getiing into all sorts of bizarre issues.

    The Transform position will only be identical to the Rigidbody position if you're not using Rigidbody interpolation. If you are using interpolation then the Transform position will be moving towards the new Rigidbody position per-frame. This is why, when using physics, you should not focus on the Transform position as the authoritative pose. That is held by the Rigidbody via its position/rotation properties.

    You are free to continuously specify MovePosition per-frame but it's wasteful as it won't do anything until the simulation steps so only the last one will do anything.

    You are free to run the simulation per-frame and turn-off interpolation completely. This means that the physics/transforms are always in-sync. The downside being that you're running a variable time-step simulation which drastically reduces any kind of determinism (not that it's completely deterministic anyway) and that stacks of objects may not be as stable. That said, for many games this makes no difference and for yours that might be true too. Per-frame simulation also means the fixed-update stuff can be ignored.

    Physics2D.AutoSimulation
    Physics2D.Simulate

    I would suggest however reading through my previous post and following the advice there first as manual simulation just gives you more control, it's not about solving jitter.
     
    DeadZombieee and LiterallyJeff like this.
  16. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    Thank you!
     
  17. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    I played with some things again:
    1. Implemented movement with MovePosition/AddForce/Velocity/CharacterController
    2. Turned V-Sync on/off
    3. Applied different interpolation settings to Rigidbody2d
    4. Downloaded a project from Brackeys tutorial (it uses CharacterController), build it up and still see this movement artifacts.

    I gravitate towards idea that maybe the problem is not in unity? :) Display/NVidia or Windows settings?
     
    Last edited: Jan 9, 2020
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    Sorry but this isn't a 2D physics issue. Simply add a Sprite to a GameObject (no Rigidbody2D) and move that via the Transform. Does it have the "issue" still? It's still not clear what your "artifacts" are. Are they screen-blur due to your monitor which you show in one video or that terrible erratic motion you show in the first?

    If whatever you have works by movement via 3D physics then it'll work in 2D physics. I mean, you're only talking about a Rigidbody moving here. If it's smooth with 3D that means you're using "Interpolation" mode for the 3D Rigidbody. The same for a Rigidbody2D works in an identical way.

    It's not clear to me what your issue actual is or what you're doing. You've not replied regarding the issues highlighted in your movement code above which is wrong. That code for 3D physics won't work either.

    I may be completely wrong in this and I'm sorry if it's not the case but your list of things you've tried sound like guesses without addressing what the issue actually is.

    I'd like to help so send me a simple test project and I'll take a look. Hopefully you've addressed the issue with your code above already.
     
    Last edited: Jan 9, 2020
    DeadZombieee likes this.
  19. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    How can I deliver it? Git/google drive?

    I gravitate towards idea that maybe the problem is not in unity? :) Display/NVidia or Windows settings?
    But first I gotta check out Sprite and Transform without rigidbody2D
     
  20. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    Please do that but also please comment on what the artifact is. Are you talking about motion blur which has been commented on a few times above or obvious erratic motion? Either way, you've already stated that using 3D physics the artifact goes away which I find very odd indeed.
     
    DeadZombieee likes this.
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    However you like. You can also upload it here.
     
    DeadZombieee likes this.
  22. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    You were right, the artifacts are visible without using Rigidbody2D.
    I've uploaded project here
    And that's how it looks like on my display

    1. Artifact one = motion blur
    2. Artifact two = small micro jittering. It looks like object moves a bit discretely. Doesn't look smooth.
     
  23. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    It's like it goes forward and sometimes for a couple of pixels go back
     
  24. Wezai

    Wezai

    Joined:
    Dec 30, 2016
    Posts:
    74
    Have you tried to check if this is an editor only occurrence? Build the game and test it in your target platforms. I've had that type of problems in the past but in the builds the movement was smooth with no jittering or ghosting.
     
    DeadZombieee likes this.
  25. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    I had this jittering problem in GMS2 when I was working on Windows 7. The solution was very unexpected. I had to apply Aero theme in Windows 7 settings. Since it uses compositing window manager the graphics started handle differently and after that my player movement become perfect! But now I'm working on Windows 10, maybe it requires some customization as well?
     
    Last edited: Jan 9, 2020
  26. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    I tried to build the project, but only for my Windows platform.
     
  27. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    As discussed, that's your screen. Not Unity or anything else. You need to get a faster monitor if you want to reduce that. If you capture the video directly instead of using an external camera, there will not be any motion blur (on a frame-by-frame basis, that is...you will still see it when played at a normal speed because your screen is slow).

    And the results were...?

    --Eric
     
    DeadZombieee likes this.
  28. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    Unfortunately, the same :(
     
  29. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    Sounds reasonable, but in GMS2 I don't see motion blur. And it also stops if I set Rigidbody2D interpolation to "none". But in this case the effect of twitching/jittering increases at times.
     
  30. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, it can't be Unity. You can prove this by capturing the video directly instead of filming it, and looking at the individual frames. Note that different graphics exhibit the effect to greater or lesser degrees largely depending on contrast, so you probably just haven't noticed it before, but it's been there all along. My monitor has an OK-ish response time and effects like these generally can't be seen, but with a texture that's high-contrast enough, such as a bright metal chain link fence on a black background, it's definitely visible.

    Anyway, if messing around with window compositing fixes the jittering issue, that suggests it's not a Unity problem either.

    --Eric
     
    DeadZombieee likes this.
  31. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    Tried your project. No jitter in a build. It's blurry on one of my slow response displays and non-blurry on a fast response one. As described above, the blurry artifact is the display, not what's being rendered so it's impossible to capture it via a screengrab.

    Note I took off the Rigidbody2D and just drove the transform using:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player1Movement : MonoBehaviour
    4. {
    5.     public float Amplitude = 5.0f;
    6.     public float Speed = 90.0f;
    7.  
    8.     private float m_Phase;
    9.  
    10.     void Update()
    11.     {
    12.         transform.position = new Vector3(Amplitude * Mathf.Cos(m_Phase), Amplitude * Mathf.Sin(m_Phase), 0.0f);
    13.  
    14.         m_Phase += Speed  * Mathf.Deg2Rad * Time.deltaTime;
    15.     }
    16. }
    17.  
    You're also using the pixel-perfect camera script which reduces the effective resolution so that can make it look jittery depending on how you set it up. I turned it off and it was better in the current set-up but you can obviously use it as long as you configure it correctly. Right now it's a low resolution (large pixels) so the adjacent Sprite positions are fairly large.

    Anyway, not physics so I've not got much else to say that hasn't already been said.
     
    igmac and DeadZombieee like this.
  32. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    Wow! Actually, when I applied your code I saw the same thing. Jittering is gone, only blurry ghosts are still there. Does it mean the problem is in my movement script? Have you tried to launch the game with my script? Was there any jittering in the movement?
     
  33. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    Yes, of course. No jittering although as I said, the pixel perfect camera affects it. Try disabling that and producing a build.
     
    DeadZombieee likes this.
  34. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    Thank you! I got rid off Pixel Perfect Camera and it looks much better now. When player moves, his pixels now might be not straight square on some point and it also gives sort of a "liquid effect". I used PPC to get it out of the way and now I have to solve it without PPC somehow.

    Again, Thank you so much for your help and for your time! It seems like we broke the current stalemate and I can go further in 2D development in Unity.
     
  35. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    UPD: By the way... movement is smooth only in build. Playmode is still jerky.
     
  36. DeadZombieee

    DeadZombieee

    Joined:
    Mar 10, 2019
    Posts:
    32
    One more update here:
    I made a build for Android platform and good news - player movement on my Huawei P20 Pro is the most smoother I've ever seen. Exactly as I expected from the video game: no blur in motion, no jitter, no 'Liquid effect', everything is perfect! )) But only if the app has been installed. In remote mode it's literally unplayable ;(
     
    MelvMay likes this.
  37. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    Click "Like" on the posts for the devs that helped above; it is the coin of our people! :)

    Anyway, good luck with it, glad you can move forward now.
     
    svenlisse, keyuan and DeadZombieee like this.
  38. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    I've been having this problem for a long time.
    You can assume I know how Update and FixedUpdate work -- I've tried all the interpolation settings on my Rigidbody2Ds, read a ton of the docs a 2nd time, am using the correct Time.deltaTime vs Time.fixedDeltaTime, have moved my player with a frictionless PhysicsMaterial2D, disabling my Pixel Perfect Camera and just stealing the orthographic camera size, etc. --
    Nothing worked. (Though I am keeping my Pixel Perfect Camera off for now).

    After almost giving up, I decided I'll just run my physics in Update by going to Edit > Project Settings > Physics 2D and setting Simulation Mode to Script, and manually simulating my PhysicsScene2Ds when I'd like in the Update loop.
    This fixed everything haha.
    So far, my player movement is working better and smoother than ever, despite calling physicsScene2D.Simulate(dt) with a varying dt across each frame.

    My project is in Unity 2020.1.4f1.
     
  39. sHassan-XansrTech

    sHassan-XansrTech

    Joined:
    May 15, 2023
    Posts:
    16
    Help me the Ghost effect, motion blur, jittering issue on Unity 2d with a fast moving ball Game
    20230522_182824.jpg
     

    Attached Files:

  40. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Please start a new thread with your problem instead of reviving old ones. Also include more information other than some pictures or your monitor, like the movement code.
     
    ModLunar and MousePods like this.
  41. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,328
    Please don't hijack threads and make duplicates.

    Thanks.
     
    ModLunar likes this.
Thread Status:
Not open for further replies.