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 Darkmatter520 · Mar 19, 2017 at 07:53 PM · movementcamera rotatefirst-personridgidbody

Screen jitters when looking around as the Rigidbody moves

I have a basic first person character with a simple script that allows movement and looking around with the mouse. The problem occurs when moving around AND looking around. The screen jitters and the edges of objects become shaky. If I am only moving but not using the mouse there is no problem. Likewise, if I am looking around but not moving there is no issue. Here is the code I am using on the character:

 public class PlayerControl : MonoBehaviour {
     public float mSpeed;
     public float lSpeed;
     public Rigidbody rb;
     public GameObject cam;
 
     // Use this for initialization
     void Start () {
         QualitySettings.vSyncCount = 0; 
     }
     
     // Update is called once per frame
     void Update () {
         looking();
     }
 
     void FixedUpdate()
     {
         movement();
     }
 
     void movement()
     {
         float moveHor = Input.GetAxis("Horizontal");
         float moveVert = Input.GetAxis("Vertical");
 
         rb.AddRelativeForce(moveHor * mSpeed, 0, moveVert * mSpeed);
     }
 
     void looking()
     {
         float lookHor = Input.GetAxis("Mouse X");
         float lookVert = Input.GetAxis("Mouse Y");
 
         transform.Rotate(0, lookHor * lSpeed, 0);
         cam.transform.Rotate(-lookVert * lSpeed, 0, 0);
     }
 }

Thank you for the help

[1]: /storage/temp/90247-desktop-03192017-11165005.png

desktop-03192017-11165005.png (440.5 kB)
Comment
Add comment · Show 2
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 RobAnthem · Mar 19, 2017 at 08:00 PM 0
Share

You shouldn't use any kind of AddForce for movement, use velocity. Also for both your Looking and $$anonymous$$oving scripts, they should both be called from FixedUpdate. Also, you're look and movement should be scaled by Time.fixedDeltaTime.

avatar image Darkmatter520 RobAnthem · Mar 20, 2017 at 11:58 PM 0
Share

I moved both methods into the Fixed Update and still get jittering. How can I use velocity for movement ins$$anonymous$$d of Addforce while being scaled by Time.deltaTime? Thanks for the help

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Commoble · Mar 19, 2017 at 07:58 PM

Your camera movement should be in the same update cycle as the object it's following. Update and FixedUpdate occur at different rates relative to each other, and this ratio of updates-per-second isn't constant, and having a camera moving in Update following something that moves via physics or FixedUpdate can cause jittering.

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 Darkmatter520 · Mar 21, 2017 at 12:00 AM 0
Share

Still jitters if both are in the same update cycle. RobAnthem mentioned using velocity ins$$anonymous$$d of AddForce but I am not sure how to do that and scale it by time.DeltaTime. Thanks for the help

avatar image
1

Answer by RobAnthem · Mar 21, 2017 at 02:01 AM

By moving and object with AddForce, it will automatically gain jitters if it is not a single force event. The inertia of the force slows down each frame, but then force gets added back in. This causes the entity to get launched forward even faster, and then the inertia tries to slow back down. Then compound this with Update and things get "jittery" FixedUpdate is called a fixed number of frames. Like if we were to basically create a system timer and use it for our own callbacks, we could make a custom "Update" method. The variance in the amount of time from Update and FixedUpdate is that Fixed Update occurs at intervals, Update occurs again the moment the previous update is complete.

So we use velocity, scaled over a fixed period of time. Creating more consistency. Moving something by Velocity is as simple as

 public Rigidbody rigidbody;
 public float speed;
 private float vertical;
 
 void FixedUpdate()
 {
     vertical = Input.GetAxis("Vertical");
     rigidbody.velocity = (transform.forward * vertical) * speed * Time.fixedDeltaTime;
 }

For strafing

     void FixedUpdate()
     {
         vertical = Input.GetAxis("Vertical");
         horizontal = Input.GetAxis("Horizontal")
         rigidbody.velocity = ((transform.forward * vertical) + (transform.right* horizontal)) * speed) * Time.fixedDeltaTime;
     }


To scale it over time, here's a mock example, plenty of ways to do it though.

     private float velocity;
     public float maxVelocity;
     public float acceleration;
     void FixedUpdate()
     {
         vertical = Input.GetAxis("Vertical");
         horizontal = Input.GetAxis("Horizontal")
         if (vertical != 0 || horizontal != 0)
         {
             if (velocity <= maxVelocity)
             {
                 velocity += acceleration * Time.fixedDeltaTime;
             }
         }
         else if (velocity > 0)
         {
             velocity -= acceleration * Time.deltaTime;
             if (velocity < 0)
                 velocity = 0;
         }
         rigidbody.velocity = ((transform.forward * vertical) + (transform.right* horizontal)) * velocity) * Time.fixedDeltaTime;
     }
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 Darkmatter520 · Mar 23, 2017 at 12:22 AM 0
Share

Thank you! This helped a lot!

avatar image RobAnthem Darkmatter520 · Mar 24, 2017 at 07:05 PM 0
Share

Glad to help, if this was the answer, go ahead and mark it as answered.

avatar image
0

Answer by ifurkend · Mar 23, 2017 at 02:30 AM

Try enabling interpolate or extrapolate.

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

6 People are following this question.

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

Related Questions

What is the best way to move a character for an FPS? 1 Answer

Player not following touch after camera is rotated? 0 Answers

Camera defaulting to -90 Y 0 Answers

only rotate on x axis 1 Answer

3D RTS camera 3 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