- Home /
Stuttering Player Movement
Hey guys, currently when my player is moving he is stuttering. I don't know how I should fix that. Hope that someone has a solution.
Here's my movement script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour {
//Variables public CharacterController controller; public float gravity = -9.81f; public Transform groundCheck; public float groundDistance = 0.4f; public LayerMask groundMask; public float jumpHeight = 3f; public float speed;
Vector3 velocity; bool isGrounded; private bool canDoubleJump;
void Start()
{
}
void Update()
{
//Gravity & GroundCheck
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -4f;
}
//Dash
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speed = 14;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
speed = 7f;
}
//Movement
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
velocity.y += gravity * Time.deltaTime;
controller.Move(move * speed * Time.deltaTime);
controller.Move(velocity * Time.deltaTime);
//Gravity changing with button
if(Input.GetKey("2"))
{
gravity = -0.2f;
}
if(Input.GetKey("1"))
{
gravity = -20f;
}
//Jump / DoubleJump
if(isGrounded)
{
canDoubleJump = true;
}
if(Input.GetButtonDown("Jump"))
{
if (isGrounded) {
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
} else {
if (canDoubleJump == true) {
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
canDoubleJump = false;
}
}
}
} }
Answer by BlueMango10 · Mar 17 at 10:06 PM
Try changing Update to FixedUpdate. You should generally use FixedUpdate whenever you call physics function every frame.
Answer by antonsimmerle · Mar 18 at 02:01 PM
When I use FixedUpdate it's stuttering even more.
Try changing Time.deltaTime to Time.fixedDeltaTime. I don't think it will fix the issue, but it's worth a try.
Your answer
![](https://koobas.hobune.stream/wayback/20220613054321im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Jerky motion when standing on hovering object, 0 Answers
Have a character face their moving direction? 1 Answer
Player Controller moves automatically forward. cant stop. 1 Answer
My Player is crossing the ground through air 1 Answer
How to Logically Match Ground Slope While Using This Code? 1 Answer