Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 xGoldenK · Aug 06, 2020 at 08:43 PM · animationphysicsplayercontrollervibration

Player vibrates back and forth when moving

Hello, I've tried creating a new project on Unity3D but I've noticed that whenever I move my player after a while, it starts vibrating back and forth. I've been using a different type of Cinemachine Camera (Virtual Camera, I'll give more details if this can cause the problem) and suddenly that happens. Here's the code that manages Player Movement using UnityEngine;

 public class PlayerMovement : MonoBehaviour
 {
     [Header("Object References")]
     public CharacterController controller;
     public new Camera camera;
     public Animator animator;
 
     [Header("Variables")]
     public float walkingSpeed = 5f;
     public float sprintSpeed = 10f;
     public float turnTime = 0.13f;
     private float currentSpeed;
     private float turnVelocity;
 
     [Header("Variables (Read-Only)")]
     public float verticalSpeed = 0f;
     public const float gravityConstant = 9.81f;
 
     public enum State
     {
         Idle = 0,
         Running = 1,
         Jumping = 2
     }
 
     private void Start()
     {
         camera = Camera.main;
         currentSpeed = walkingSpeed;
     }
 
     private void Update()
     {
         if(Input.GetKeyDown(KeyCode.LeftShift))
         {
             currentSpeed = sprintSpeed;
         }
         if(Input.GetKeyUp(KeyCode.LeftShift))
         {
             currentSpeed = walkingSpeed;
         }
     }
 
     void FixedUpdate()
     {
         float horizontal = Input.GetAxisRaw("Horizontal");
         float vertical = Input.GetAxisRaw("Vertical");
         Vector3 moveDir = new Vector3(horizontal, 0f, vertical).normalized;
 
         /*
          * TODO: TRANSFER SPEED, RUNNING AND GRAVITY METHODS/VARIABLES TO 
          * TEMPLATE FOR OTHER CHARACTERS (ENEMIES AS WELL)
          */
 
 
         if(moveDir.magnitude >= 0.1)
         {
 
             float targetAngle = Mathf.Atan2(moveDir.x, moveDir.z) * Mathf.Rad2Deg + camera.transform.eulerAngles.y;
             float finalAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnVelocity, turnTime);
             transform.rotation = Quaternion.Euler(0f, finalAngle, 0f);
 
             Vector3 finalMoveDir = Quaternion.Euler(0f, targetAngle, 0f).normalized * Vector3.forward;
             controller.Move(finalMoveDir * currentSpeed * Time.deltaTime);
             animator.SetInteger("State", (int)State.Running);
         }
         else
         {
             animator.SetInteger("State", (int)State.Idle);
         }
         applyGravity();
     }
 
     //GRAVITY HANDLER
     public void applyGravity()
     {
         switch(controller.isGrounded)
         {
             case true:
                 verticalSpeed = 0f;
                 break;
             case false:
                 verticalSpeed -= gravityConstant * Time.deltaTime;
                 break;
         }
 
         Vector3 gravityDir = new Vector3(0f, verticalSpeed, 0f);
         controller.Move(gravityDir * Time.deltaTime);
 
     }
 }

This is the part that actually does the moving:

    void FixedUpdate()
     {
         float horizontal = Input.GetAxisRaw("Horizontal");
         float vertical = Input.GetAxisRaw("Vertical");
         Vector3 moveDir = new Vector3(horizontal, 0f, vertical).normalized;
 
         /*
          * TODO: TRANSFER SPEED, RUNNING AND GRAVITY METHODS/VARIABLES TO 
          * TEMPLATE FOR OTHER CHARACTERS (ENEMIES AS WELL)
          */
 
 
         if(moveDir.magnitude >= 0.1)
         {
 
             float targetAngle = Mathf.Atan2(moveDir.x, moveDir.z) * Mathf.Rad2Deg + camera.transform.eulerAngles.y;
             float finalAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnVelocity, turnTime);
             transform.rotation = Quaternion.Euler(0f, finalAngle, 0f);
 
             Vector3 finalMoveDir = Quaternion.Euler(0f, targetAngle, 0f).normalized * Vector3.forward;
             controller.Move(finalMoveDir * currentSpeed * Time.deltaTime);
             animator.SetInteger("State", (int)State.Running);
         }
         else
         {
             animator.SetInteger("State", (int)State.Idle);
         }
         applyGravity();
     }
Comment
Add comment · Show 1
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 moatdd · Aug 06, 2020 at 11:33 PM 0
Share

I also found this excellent article regarding stutter caused by timestep issues:

https://www.kinematicsoup.com/news/2016/8/9/rrypp5tkubynjwxhxjzd42s3o034o8

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by moatdd · Aug 06, 2020 at 11:30 PM

I think you're calling Controller.Move TWICE in the same fixedupdate frame. Once at Line 64 in FixedUpdate, and again at line 88 in applyGravity().

So a couple of things... first, you might want to combine both of these moves into a single move at the end by taking your finalMoveDir and combining it with the gravityDir from applyGravity, and THEN calling the controller.Move(), because each call of controller.Move() takes collisions into account.

Second, I believe that controller.Move is meant to be applied in the Update() event rather than FixedUpdate() event. (at least most Unity Examples that I've seen indicate this)

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
avatar image
0

Answer by xGoldenK · Aug 07, 2020 at 10:24 AM

I've solved the initial problem about the camera, I've changed the cinemachine camera type to FreeLook instead of Virtual. However, I've tried implementing what you said but now whenever I press the key to jump, the player only jumps "randomly": even if I start the project and completely stand still, if I press space 6 times the player only jump a couple of times. I tried removing the controller.isGrounded and it works (but of course, the player can jump even in mid-air, but at least the rest of the code is working). Here's my current code: using UnityEngine;

 public class PlayerMovement : MonoBehaviour
 {
     [Header("Object References")]
     public CharacterController controller;
     public new Camera camera;
     public Animator animator;
 
     [Header("Variables")]
     public float walkingSpeed = 5f;
     public float sprintSpeed = 10f;
     public float turnTime = 0.13f;
     public float jumpForce = 6f;
     public State currentState;
     private Vector3 finalMoveDir;
     //VARIABLES THAT TRACK CURRENT VALUES
     private float currentJumpForce = 0f;
     private float currentSpeed;
     private float turnVelocity;
 
     public enum State
     {
         Idle = 0,
         Running = 1,
         Jumping = 2
     }
 
     private void Start()
     {
         camera = Camera.main;
         currentSpeed = walkingSpeed;
     }
 
     void Update()
     {
         //HANDLE INPUT
         if(Input.GetKeyDown(KeyCode.LeftShift))
         {
             currentSpeed = sprintSpeed;
         }
         if(Input.GetKeyUp(KeyCode.LeftShift))
         {
             currentSpeed = walkingSpeed;
         }
         if(Input.GetKeyDown(KeyCode.Space) && controller.isGrounded)
         {
             currentJumpForce = jumpForce;
         }
 
         //APPLY FORCES
         float horizontal = Input.GetAxisRaw("Horizontal");
         float vertical = Input.GetAxisRaw("Vertical");
         Vector3 moveDir = new Vector3(horizontal, 0f, vertical).normalized;
         Vector3 jumpDir = new Vector3(0f, currentJumpForce, 0f);
 
         if(moveDir.magnitude >= 0.1)
         {
 
             float targetAngle = Mathf.Atan2(moveDir.x, moveDir.z) * Mathf.Rad2Deg + camera.transform.eulerAngles.y;
             float finalAngle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnVelocity, turnTime);
             transform.rotation = Quaternion.Euler(0f, finalAngle, 0f);
 
             finalMoveDir = Quaternion.Euler(0f, targetAngle, 0f).normalized * Vector3.forward;
             SetState(State.Running);
         }
         else
         {
             switch(currentJumpForce > 0.1f)
             {
                 case true:
                     SetState(State.Jumping);
                     break;
                 default:
                     SetState(State.Idle);
                     break;
             }
         }
 
         controller.Move((finalMoveDir * currentSpeed * Time.deltaTime) + jumpDir);
         finalMoveDir = new Vector3(0f, 0f, 0f);
         currentJumpForce = 0f;
     }
 
     public void SetState(State state)
     {
         animator.SetInteger("State", (int)state);
         currentState = state;
     }
 
 }
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

408 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to have CharacterController interact with Object 1 Answer

How would I make it so an animation will play, but stop when it hits a collider? 0 Answers

3d animation controller 0 Answers

How to limit the forces applied to an object to just forward, left, and right? 0 Answers

Physics controls with look ahead. 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