Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Kitcheone · Oct 11, 2016 at 07:34 PM · cameramovementrigidbodyjerky

Why is an object's movement jerky, when the camera is also moving by following a different rigidbody?

I've seen a bunch of similar questions to this, but so far everything suggested hasn't helped me.

My set-up is as follows;

1) A capsule, that is moving with constant speed, using:

 void Update()
 {
    transform.position += new Vector3(Time.deltaTime, 0, 0);
 }

2) A sphere with a rigidbody, that is set to move on player input:

 void FixedUpdate()
 {
     if (Input.GetKey(KeyCode.D))
     {
         GetComponent<Rigidbody>().AddForce(new Vector3(1, 0, 0));
     }
 }

3) A camera, set to follow the sphere (with some z offset, to keep everything in view):

 void LateUpdate()
 {
     transform.position = Sphere.GetComponent<Rigidbody>().position + new Vector3(0, 0, -5);
 }

With the capsule using Update, the sphere using FixedUpdate and the camera using LateUpdate (as above), the following gfy shows what happens:

https://gfycat.com/BreakableSilverAlabamamapturtle

As you can see, when the camera is moving, the capsule movement is really jerky.

Alternatively, if I move the capsule code into FixedUpdate, it becomes:

https://gfycat.com/RapidSleepyAbyssiniancat

Which is better, but still not perfect. In particular, the capsule movement appears jerky in the beginning, when the camera is stationary. (This isn't very noticeable in the gfy, but trust me, it's worse in real time!)

I've tried various other combinations of Update types, along with changing the interpolation of the rigidbody, all to no avail.

Is there any way to get the capsule moving smoothly all the time, regardless of camera motion? Or am I going to have to learn to live with it?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jrocamora · Oct 11, 2016 at 08:55 PM

With Update(), the problem is framerate, so isn't moving constantly according to the display perception.

In FixedUpdate(), the capsule is moving constantly, but you're still perceiving it as moving according to the display.

The problem is actually the camera following the sphere. AddForce doesn't do the same thing as just constantly updating the position like with the capsule. The movement that you are seeing in the capsule is both the camera and the sphere moving tiny bits because of the engine physics not moving the sphere at an explicitly constant speed, but since the camera is following the sphere, the sphere looks like it's the one that isn't moving.

Comment
Add comment · Show 2 · 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
avatar image Kitcheone · Oct 11, 2016 at 10:53 PM 0
Share

Of course! I totally didn't realise the code moving the sphere would be the culprit.

Thanks for the help!

avatar image BorisOkunskiy · Apr 09, 2020 at 08:13 AM 0
Share

To be fair, I think the code moving the sphere is fine. As another answer suggests, it's the Rigidbody updating object position at fixed rate -- but camera updating its position at regular rate. The inconsistency between the two results in perceived jerkiness of rigidbody that is being followed.

avatar image
1

Answer by Prosto_Lyubo · Nov 11, 2017 at 12:47 AM

I had similar issue. Turns out that apparently RigidBody updates its position only in FixedUpdate (along with the physics and applied forces). In Inspector I set my rigid body's Interpolation to interpolate. The jagged movement turned into a pretty smooth one. To be sure there are no rapid changes in the object movement I also smoothed the force applied to the object (but that may not be desired behaviour - it's case dependant but worked very well for me):

 private Vector3 lastAcceleration = Vector3.zero;
 private const float damping = 0.7f;
 [SerializeField]
 private RigidBody2D rb;
 private void FixedUpdate()
 {
     Vector3 acceleration = Vector3.zero;
     acceleration = /* Calculate Your acceleration here */
     lastAcceleration = Vector3.Lerp( acceleration, lastAcceleration, damping);
     rb.AddForce( lastAcceleration, ForceMode2D.Force );
 }
Comment
Add comment · Show 1 · 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
avatar image farmerdwight · May 08, 2021 at 03:27 AM 0
Share

Thanks, that worked for me!

avatar image
0

Answer by BorisOkunskiy · Apr 09, 2020 at 08:11 AM

As a general rule: if something needs to "follow" Rigidbody by updating transform.position, such updates should happen in FixedUpdate. Camera is not an exception, so changing LateUpdate to FixedUpdate in camera resolved all the jerkiness issues for me.


(Context: I had the same problem with trying to make camera follow the object with some smooth time via Vector3.SmoothDamp. It appeared well when setting smooth time to 0, but any positive value resulted in player being rendered inconsistently/jerky/jagged.)

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

7 People are following this question.

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

Related Questions

Very jerky camera movement with rigidbody 1 Answer

Character Moving in Elliptical Pattern 0 Answers

Question about topdown-movement and mouse-facing-foward-check 0 Answers

How can I set the movement of the rigidbodyController in the same direction of the mainCamera view? 0 Answers

How to rotate a Rigidbody without rotating the gameObject? 1 Answer


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