Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
0
Question by nexteon1 · Jul 30, 2020 at 05:38 PM · rigidbodycamera-movementmovement scriptplayer movementjitter

How do I stop my player or camera from jittering during player movement?

In my game, I have a third person character. The player moves with a rigidbody and has isKinematic set off with interpolate on. In the script, it moves with Rigidbody.MovePosition with the y position froze. Here's my player script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
    public float speed;
 
    public Rigidbody rb;
 
    public Transform camPos;
 
    // Start is called before the first frame update
    void Start()
    {
       rb = GetComponent<Rigidbody>();
       camPos = Camera.main.transform;
    }
 
    private void FixedUpdate()
    {
       if (Input.GetKey("w"))
       {
           rb.MovePosition(transform.position + (camPos.forward * speed * Time.fixedDeltaTime));
       }                                         
       if (Input.GetKey("s"))
       {
           rb.MovePosition(transform.position + (-camPos.forward * speed * Time.fixedDeltaTime));
       }
       if (Input.GetKey("d"))
       {
           rb.MovePosition(transform.position + (camPos.right * speed * Time.fixedDeltaTime));
       }
       if (Input.GetKey("a"))
       {
           rb.MovePosition(transform.position + (-camPos.right * speed * Time.fixedDeltaTime));
       }
 
       rb.velocity = Vector3.zero;
    }
 }

For my camera, I have it lookat the player and lag behind smoothly. Here's that script:

 //Hakeem Thomas
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CamPos : MonoBehaviour
 {
    public Transform target;
    public float smoothTime = 0.5F;
    private Vector3 velocity = Vector3.zero;
 
    public GameObject player;
 
    private float mouseX, mouseY;
    public int mouseX_Speed, mouseY_Speed;
    
    //mouseSensitivity
    public int turnSpeed;
 
    void getMouseXY()
    {
        mouseX += Input.GetAxis("Mouse X") * smoothTime;
        mouseY -= Input.GetAxis("Mouse Y") * smoothTime;
    }
    void LateUpdate()
    {
        getMouseXY();
        // Define a target position above and behind the target transform
        Vector3 targetPosition = target.TransformPoint(new Vector3(0, 1.5f, -2.5f));
 
        // Smoothly move the camera towards that target position
        transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
        transform.LookAt(target);
 
        target.rotation = Quaternion.Euler((mouseY * mouseY_Speed), (mouseX * mouseX_Speed), 0);
        player.transform.rotation = Quaternion.Euler(0, (mouseX * turnSpeed), 0);
 
    }
 }

My issue is that whenever the player moves either the camera/player jitters. I have debugged the player transform y position to make sure it wasn't moving up or down(it doesn't). To help with this issue I have either turned down the Fixed Timestep under Time in the project settings or put the code in the camera script is fixed update instead of late update(which I have researched is an issue). I know MovePosition for the player isn't the right way to move, but I don't know how to make the player move in the direction the camera is facing with rb.velocity. I suspect the issue may lay in the player script with variable camPos. Either way, I would like to stop the jittering.

Comment
Add comment · Show 3
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 davidcox70 · Jul 30, 2020 at 06:03 PM 0
Share

Does it improve if you make your camera follow script run in FixedUpdate(), rather than LateUpdate()?

avatar image nexteon1 davidcox70 · Jul 30, 2020 at 06:10 PM 0
Share

Yes, It does.

avatar image davidcox70 nexteon1 · Jul 30, 2020 at 06:13 PM 0
Share

Then the jittering is caused by the fact that LateUpdate() gets called at a different frame rate to FixedUpdate(). This beaks the sync between the camera move and the player move.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DoubleDotDee · Jul 30, 2020 at 06:37 PM

@nexteon1 My game was 2D and the script was very different but i fixed it by increasing or decreasing the speed of the camera.

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

193 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

Related Questions

Laggy FPS Camera (attached to rigid body player), I have tried everything. Unity wizards need to be summoned for this one. 0 Answers

Rigidbody movement in direction of camera? 1 Answer

How to stop input after hitting a certain velocity 1 Answer

Camera shaking/jittering? 2 Answers

RB Interpolation 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