I'm having a problem with my object when it jumps. I'm working with this code (I'm using unity):
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 10f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update ()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -4f * gravity);
}
velocity.y = gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
It works perfectly for what I want, a third person character movement, however, when I play on a lower framerate, the jump height is smaller, so instead of multiplying gravity and velocity by Time.deltaTime on void Update, I moved that operation to void FixedUpdate(), so the code look like this:
void FixedUpdate()
{
velocity.y = gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
It works perfect too and the jump height is consistent on different framerates! but when my character jumps it stutters while it's on air, there's no stutter in any other action, only when it jumps. I'd like to know why it Stutters and if there's a solution for this problem.
EDIT: I found a solution for a framerate independent jump height and without using FixedUpdate(), in velocity.y I can multiply by 0.5 like this: velocity.y = gravity * 0.5f * Time.deltaTime; and then copy this line of code and paste it again under controller.move. It should look like this:
//gravity
velocity.y = gravity * 0.5f * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
velocity.y = gravity * 0.5f * Time.deltaTime; // Calling this again to make the jump framerate independent
Unfortunately due to my limited knowledge on coding/programming I can't give a clear explanation on why does this work. This also doesn't answer to the main question of this post "Why does using FixedUpdate makes my object stutter?" I think it's just because Update and FixedUpdate runs at different framerates, and if I split the calculation of the physics in different methods it'll cause this "stutter" because one part of one method is trying to catch up with the other. Hopefully someone more experienced can give a clear answer to this ^^'
CodePudding user response:
I'm thinking that you might not need to move it outside the Update function. Your velocity.y is multiplied by delta time two times, maybe if velocity.y = gravity * Time.deltaTime; is replaced by velocity.y = gravity; Your jump height might be consistent.
If not, then i would also try to move the jump check into the fixedupdate. Not sure if this will help.
Update and fixedupdate runs at different framerates. FixedUpdate is also called before the Update method. This has sometimes cause stuttering for me because calculations are done at different times. https://docs.unity3d.com/Manual/ExecutionOrder.html#UpdateOrder
CodePudding user response:
You seem to have used the example from the documentation, but I'm not sure if they're handling this in the best way. However, assuming it is a good way, the problem in your code seems to be the use of Time.deltaTime
instead of Time.fixedDeltaTime
in FixedUpdate()
. I think that should fix it, but maybe there is a second problem.