- Home /
2d platformer player jitter when add velocity
Hi,
I'm trying to make platformer player that moving forward and backward.
Player is simple sprite
Here is my code:
private void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
But when I try to move player I feel jitter (player not smooth moving) . Can problem be some settings in unity or problem is in code ?
When I use follow player script player moves smooth but then camera jitter (not moving smooth) Here is my folow camera code:
void FixedUpdate()
{
Vector3 playerPos = player.position;
playerPos.z = transform.position.z;
transform.position = Vector3.SmoothDamp(transform.position, playerPos, ref velocity, smoothTime);
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minCameraPos.x, maxCameraPos.x),Mathf.Clamp(transform.position.y, minCameraPos.y, maxCameraPos.y),
Mathf.Clamp(transform.position.z, minCameraPos.z, maxCameraPos.z));
}
Please help me. Thanks!
I think you're missing delta time in your movement, that could be the reason.
Answer by kubajs · Mar 26, 2020 at 10:29 PM
Feel free to set rigidbody.velocity in Update method. Do a camera follow in LateUpdate. Everything will be smooth as expected. Also test that in built mode, not regular play mode as you won't usually eliminate jittering completely in play mode. Just CTRL + B and run in full screen. No need to set rigidbody.velocity in FixedUpdate as velocity can be set at any time, doesn't matter whether it's set in fixed intervals or not. Your problem is caused purely by the camera follow script. FixedUpdate method is not appropriate for this. When modifying camera transform, just run it in LateUpdate as LateUpdate always execute after Update. Move other than camera transforms in Update. Also always try to read user input in Update instead of FixedUpdate. In your case it's not a big issue but in case you read GetButtonDown (one time input), you'll realize it sometimes doesn't work (in the case input was read in the frame which was skipped by fixed update).
Thanks, I try everything but I still have jitter . I think problem might me in Unity or project settings,
Try to play with Rigidbody2D interpolation, it might improve it a bit.