• 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
Question by gkepets · Aug 13, 2019 at 11:27 PM · rigidbodyfollowjitter

Rigidbody jittering with camera follow

Hi

I have a rigidbody player that is being moved by setting the velocity and rigidbody.MoveRotation. (Example code below)

 void FixedUpdate(){
         r.MoveRotation(r.rotation * newRot);
         r.velocity = transform.forward * currentSpeed;
 }


I also have a camera following this player with this script:

 using UnityEngine;
   using System.Collections;
   
   [AddComponentMenu("Camera-Control/Smooth Follow 3D CSharp")]
   
   public class SmoothFollow3D : MonoBehaviour
   {
       
   
       // The target we are following
       public Transform target;
       // The distance in the x-z plane to the target
       public float distance = 10.0f;
       // the height we want the camera to be above the target
       public float height = 5.0f;
       // How much we 
       public float heightDamping = 2.0f;
       public float rotationDamping = 3.0f;
   
       void LateUpdate ()
       {
           // Early out if we don't have a target
           if (!target)
               return;
       
          Vector3 followpos = new Vector3(0.0f, height, -distance);
          Quaternion lookrotation = Quaternion.identity;
     
          lookrotation.eulerAngles = new Vector3(0.0f, 0.0f, 0.0f);
  
          Matrix4x4 m1  = Matrix4x4.TRS(target.position, target.gameObject.GetComponent<Rigidbody>().rotation, Vector3.one);
          Matrix4x4 m2  = Matrix4x4.TRS(followpos, lookrotation, Vector3.one);
          Matrix4x4 combined  = m1 * m2;
          
          // THE WAY TO GET POSITION AND ROTATION FROM A MATRIX4X4:
          Vector3 position = combined.GetColumn(3);
          
          Quaternion rotation = Quaternion.LookRotation(
          combined.GetColumn(2),
          combined.GetColumn(1)
          );
  
  
           Quaternion wantedRotation = rotation;
           Quaternion currentRotation = transform.rotation;
  
           Vector3 wantedPosition = position;
           Vector3 currentPosition = transform.position;
        
           currentRotation = Quaternion.Lerp(currentRotation, wantedRotation, rotationDamping * Time.deltaTime);
           currentPosition = Vector3.Lerp(currentPosition, wantedPosition, heightDamping * Time.deltaTime);
       
          transform.localRotation = currentRotation;
          transform.localPosition = currentPosition;
           
          
       }
   }


(I have my own smooth follow that looks pretty similar but I grabbed this on off the internet).

No Matter what I do my player still jitters when I rotate it (rotation is based on the mouse position on the screen). I tried interpolate/extrapolate, I tried lateupdate on the camera and smoothdamp instead of lerp.

How do I fix this? What is the ideal setup for something like this?

Thanks!

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by tuinal · Aug 14, 2019 at 01:41 AM

The MouseOrbitImproved script floating round the net (quick google) is a solid starting point for a 3rd person camera. The script you've got probably works but it's an overcomplicated way of doing a camera follow / rotate.

I think in terms of fixing this, currentRotation and currentPosition want to be from the frame before, not the current frame. Move their declarations outside of the update method, then remove the lines you're setting them with (except the 2 at the very end of the method - and you might need to set them to transform.position/rotation in Start() to avoid a null reference), and give that a try?

Comment
gkepets

People who like this

1 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 gkepets · Aug 14, 2019 at 02:42 AM 0
Share

@tuinal I tried using the previous frame rotation/position and I still get a slight jitter.... Whats the right way to go about this? It seems like everyone has a different solution... From what I understand camera in lateUpdate, player physics in fixedUpdate and Rigidbody Interpolate is the correct way to go. But then how can objects (ie Trailrenderers) attached to the rigidbody be rendered correctly if they are using update but being interpolated? Thanks again for the answer!

I changed the fixed timestamp to 0.015 and its way better, but that is definitely not the right way to fix this...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

149 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

Related Questions

Rigidbody following a spline 0 Answers

How to make a camera follow a sphere without rotating with it. 1 Answer

Character Controller movement jittery while standing on a rigidbody 1 Answer

How to have an object inherit rotation of player 1 Answer

Rigidbody projectile jittering 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