Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sadzony · Jul 01, 2019 at 08:37 PM · camera2dpixel

Objects moving relative to the player appear to be stuttering/jittering

In this project, I clip all the positions of my sprites so that the sprites are aligned perfectly by the pixels. I do the clipping by taking the rigidbody position of an object and using that Vector3 to create a transform.position which is a multiple of 1/sprite's PPU and applying that position to just the sprite and the colider of the object. After updating the camera's position with the position of the clipped components, I achieve this crisp effect: https://gyazo.com/efe2b4aca6ff0450e326bc256b51718c However, the problems begin when I introduce another moving object into the mix. The objects' fixedUpdates' aren't syncing with each other which means that they're moving in 2 cycles, creating jitter. I've been trying to sort this out all day, but there was no success. Here are some more gifs to visualize the problem:

  • Other object moving at a lower speed, same direction:

https://gyazo.com/490b44d6011c1150e55d5907865b2307

  • Object with camera stationary, other object moving (everything working nicely in this situation):

https://gyazo.com/2da9ff9440ad4a3000c7ea9ffbb57af2

  • Camera stationary while both objects moving:

https://gyazo.com/6392966a0a5fe070da99bfe3e3bea5c9

  • Slowed time scale showing that the updates are not in sync:

https://gyazo.com/ab2fabe857a7fb8a3d2930ddd3b6391c

The code making the objects go into motion:

     private void Awake()
     {
     physicsPosition = transform.position;
     }
     private void FixedUpdate()
     {
         Vector2 force = ComputeForce();
         Vector2 acceleration = new Vector2(force.x / mass, force.y / mass);
         velocity += acceleration*Time.fixedDeltaTime;
         physicsPosition = new Vector3(physicsPosition.x + 0.0625f*velocity.x * Time.fixedDeltaTime, physicsPosition.y + 0.0625f * velocity.y * Time.fixedDeltaTime, physicsPosition.z);
         transform.position = RoundToPixel(physicsPosition);
     }
 
     Vector2 ComputeForce()
     {
         return new Vector2(0, mass * (-9.81f * gravityScale));
     }
 
 
     float RoundToPixel(float value)
     {
         if (PPU != 0)
         {
             float pixel = 1.0f / PPU;
             float rem = value % pixel;
             float result = value - rem;
             return result;
         }
         else
         {
             Debug.LogError("Error: Your Pixel per unit ratio is 0.");
             return 0;
         }
     }








Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Optrix · Jul 01, 2019 at 11:20 PM 0
Share

If you're playing with transform.position, did you want to consider moving your changes to Update rather than FixedUpdate?

FixedUpdate doesn't always run every frame and is ideally used as part of the built-in Unity physics system. If you're overriding position on your object, you should probably set the RigidBody to 'kinematic' (controlled by code rather than the physics engine) and take care of the physics yourself, the way you're doing with ComputeForce. In a 2D game, it's unlikely that you need a fully-fledged RigidBody for anything but collision detection anyway.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sadzony · Jul 02, 2019 at 01:11 PM

@Optrix I've tried both moving just the updating of the position of the sprite, and the updating of the rb.position with the sprite update to Update rather than FixedUpdate, however that did not work. I've found that the root of the problem lays somewhere else. Although everything updates at the same rate, since one physics body moves faster than the other, the rates on which just the sprites are updated changes. I've tried to snap the physics position to the sprite position whenever the sprite position changes, but that made my gravity not work and also made the objects stop whenever their position reaches positive values, probably due to how my rounding is handled.

Here's a gif of the physics positions represented by colliders (physics positions are not snapped here):

https://gyazo.com/3c69c6d32b3a67b6aa7a012973da4190

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

261 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

copy camera view to 2d panel and pixelate it 0 Answers

Issue with PixelPerfectCamera script and sprite masks (see images)... How to fix? 1 Answer

Questions about Unity 2D camera setup 1 Answer

Any idea for smooth camera in pixel art sprite? 1 Answer

How to letterbox the screen and have pixel perfection in a 2d game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges