Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by aderelha · Oct 28, 2021 at 07:38 PM · cameracamera-movementfirst-person-controllerjitterstutter

How do I fix Camera stuttering in first person?

I have a simple scene with a player in it. The player is a Capsule with a rigidbody and holds all scripts concerning movement, Camera movement, etc. The player-GameObject is also the parent of a Camera- GameObject, which I use for the first person perspective. The problem is the following: When I move the rigidbody of the player using force, the camera stutters. The movement loop of the player is run in FixedUpdate and the Camera movement loop is run in LateUpdate.

Here is the script for the movement:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Walking : MonoBehaviour
 {
     public Rigidbody playerRigidb;
     public Camera firstPersonCam;
     public float moveSpeed;
     float hor;
     float ver;
 
     void getInput()
     {
         hor = Input.GetAxisRaw("Horizontal");
         ver = Input.GetAxisRaw("Vertical");
     }
 
     void Walk()
     {
         playerRigidb.AddForce(transform.forward * ver + transform.right * hor, ForceMode.VelocityChange);
     }
 
     void Update()
     {
         getInput();
     }
 
     private void FixedUpdate()
     {
         Walk();
     }
 }

And here is the script to move the Camera:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FirstPersonCamera : MonoBehaviour
 {
     public Rigidbody playerRigidb;
     public Camera firstPersonCam;
     float mouseX;
     float mouseY;
     float rotX;
     float rotY;
     public float mouseRotationSpeed;
     // Start is called before the first frame update
 
     void GetInput()
     {
         mouseX = Input.GetAxisRaw("Mouse X");
         mouseY = Input.GetAxisRaw("Mouse Y");
 
         rotX += mouseY * mouseRotationSpeed;
         rotY += mouseX * mouseRotationSpeed;
         rotX = Mathf.Clamp(rotX, -90, 90);
     }
 
     void LockCursor()
     {
         if (Input.GetKey(KeyCode.Escape))
             Cursor.lockState = CursorLockMode.None;
         else
             Cursor.lockState = CursorLockMode.Locked;
     }
 
     // Update is called once per frame
     void Update()
     {      
         GetInput();       
         LockCursor();
     }
     private void LateUpdate()
     {
         firstPersonCam.transform.localRotation = Quaternion.Euler(-rotX, 0, 0);
         transform.rotation = Quaternion.Euler(0, rotY, 0);
     }
 }

I already tried:

  • Using different types of "Update"

  • Interpolation in rigidbody

  • Removing the Camera as child of the player

And I also tried searching for other answers on the internet, but nothing helped me.

Thank you in advance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WorldLink · May 10 at 05:44 PM

If you're still struggling with this, or if anyone else with a similar problem comes across this, one possible solution is to have a non-child camera following using a tightened up smoothing function. The problem results from discrepancy between the physics timestep (targeting 50 Hz) and the framerate (highly variable). Basically, every once in a while the camera updates its transform more than once before the rigidbody catches up. You can mitigate this a bit by turning on interpolation on the rigidbody, but you will still get stutter. You can find an in-depth take on the problem here. Instead, I've found it helpful to keep the camera separate and tell it to follow the player very tightly with Vector3.SmoothDamp.


For example, in this exerpt I have the player controller keep track of the camera's transform and smoothly pull it along:

 public float cameraFollowSmoothTime = 0.05f; // Tweak in inspector.
 public Vector3 cameraOffset; // Offset of the camera from the player.
 
 private Vector3 cameraVelocity;
 
 private void CameraFollow()
 {
     Vector3 targetCamPos = transform.TransformPoint(cameraOffset);
     cameraTransform.position = Vector3.SmoothDamp(cameraTransform.position,
                                                   targetCamPos,
                                                   ref cameraVelocity, cameraFollowSmoothTime);
 }

If you wanted, you could do this from a script on the camera as well, keeping track of the player's transform. You would also want to make sure to set the correct initial position for the camera in Awake() or Start(). It's similar to how a lot of 3rd person cameras work, but tightened up so that the camera is real close.


One disadvantage of this approach is that you have to move the player and camera independently in scene view. Also, any kind of teleportation would have to involve separately updating both the player's and the camera's positions.

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

207 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

Related Questions

Camera Help? 0 Answers

Jittery Movement - specific mechanics in mind 0 Answers

Camera Jitter / Stutter 1 Answer

Smoother camera in First Person Controller Prefab? 1 Answer

Moving a camera with AddFoce, or setting velocity, need help 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