0
\$\begingroup\$

Good afternoon, currently my player shakes when you move it to the left or right. I am using cinemachine 2d virtual cammera.

Here's my movement script:

using Microsoft.Cci; using UnityEngine; public class PlayerMovement : MonoBehaviour { private float horizontal; private float speed = 8f; private float jumpingPower = 5f; private bool isFacingRight = true; public bool OnGround = true; public int JumpNum = 1; [SerializeField] private Rigidbody2D rb; [SerializeField] private Transform groundCheck; [SerializeField] private LayerMask groundLayer; void Start() { rb = GetComponent<Rigidbody2D>(); } private void OnCollisionEnter2D(Collision2D collision) { OnGround = false; Debug.Log("InAir false"); } private void OnCollisionExit2D(Collision2D collision) { OnGround = true; Debug.Log("InAir True"); } void Update() { horizontal = Input.GetAxisRaw("Horizontal"); if(Input.GetKeyDown(KeyCode.W) && JumpNum < 2) { rb.velocity = new Vector2(rb.velocity.x, jumpingPower); JumpNum++; //double jump } if (Input.GetKeyDown(KeyCode.W) && !OnGround && JumpNum >= 2) { rb.velocity = new Vector2(rb.velocity.x, jumpingPower); JumpNum = 1; //if jumpnum == 2, then reset jumpNum } // if (Input.GetKeyDown(KeyCode.W) && rb.velocity.y > 0f) // { //rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f); // OnGround= false; //} //redundant Flip(); } private void FixedUpdate() { rb.velocity = new Vector2(horizontal * speed, rb.velocity.y); } //private bool IsGrounded() // { // return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer); // } //doesn't work private void Flip() { if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f) { isFacingRight = !isFacingRight; Vector3 localScale = transform.localScale; localScale.x *= -1f; transform.localScale = localScale; } } } 

I looked at similar questions such as: https://www.reddit.com/r/Unity2D/comments/o84k7x/weird_shaking_with_player_controllercinemachine/

https://stackoverflow.com/questions/72441202/picked-up-object-is-shaking-when-moving#:~:text=The%20ways%20around%20this%20all%20revolve%20around%20either,the%20same%20time%20%28moving%20your%20interaction%20to%20Update%29.

https://stackoverflow.com/questions/35158051/twitching-objects-in-unity-during-camera-movement-in-2d

But the answers to those questions did not help me.

Is there any way to fix the player's shaking when moving it left or right?

\$\endgroup\$
4
  • \$\begingroup\$ Do you mean with shaking that you are calling Flip() every single Update? \$\endgroup\$ Commented Jul 19, 2023 at 15:09
  • \$\begingroup\$ Yea, I tried changing the void Update() to void LateUpdate() but it didn't change anything. \$\endgroup\$ Commented Jul 19, 2023 at 15:12
  • \$\begingroup\$ In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour - use AddForce instead. Do not set the velocity of an object every physics step, this will lead to unrealistic physics simulation. \$\endgroup\$ Commented Jul 19, 2023 at 15:56
  • \$\begingroup\$ Doing the AddForce doesn't solve the issue, maybe it's when the sprite flips from left to right? \$\endgroup\$ Commented Jul 20, 2023 at 3:51

1 Answer 1

0
\$\begingroup\$
using Microsoft.Cci; using UnityEngine; public class PlayerMovement : MonoBehaviour { private float horizontal; public float speed = 1f; private float jumpingPower = 5f; private bool isFacingRight = true; public bool OnGround = true; public int JumpNum = 1; [SerializeField] private Rigidbody2D rb; [SerializeField] private Transform groundCheck; [SerializeField] private LayerMask groundLayer; void Start() { rb = GetComponent<Rigidbody2D>(); } private void OnCollisionEnter2D(Collision2D collision) { OnGround = false; Debug.Log("InAir false"); } private void OnCollisionExit2D(Collision2D collision) { OnGround = true; Debug.Log("InAir True"); } void Update() { horizontal = Input.GetAxisRaw("Horizontal"); if(Input.GetKeyDown(KeyCode.W) && JumpNum < 2) { rb.AddForce(Vector2.up * jumpingPower, ForceMode2D.Impulse); //rb.velocity = new Vector2(rb.velocity.x, jumpingPower); JumpNum++; //double jump } if (Input.GetKeyDown(KeyCode.W) && !OnGround && JumpNum >= 2) { rb.AddForce(Vector2.up * jumpingPower, ForceMode2D.Impulse); JumpNum = 1; //if jumpnum == 2, then reset jumpNum } // if (Input.GetKeyDown(KeyCode.W) && rb.velocity.y > 0f) // { //rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f); // OnGround= false; //} //redundant } private void FixedUpdate() { //rb.AddForce(Vector2.right * speed); rb.AddForce(new Vector2(horizontal * speed, 0f), ForceMode2D.Impulse); Flip(); } //private bool IsGrounded() // { // return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer); // } //doesn't work private void Flip() { if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f) { isFacingRight = !isFacingRight; Vector3 localScale = transform.localScale; localScale.x *= -1f; transform.localScale = localScale; } } } 

I figured it out. Thank you so much for the help!

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.