Unity skipping input
Hi! I'm trying to make a very simple jump mechanic while also trying to follow good programming practices according to what I've been learning recently. I'm currently detecting my input in the update method and using a bool as a flag to trigger the add force code in the fixed update method, however sometimes there's some sort of "input skipping" or something like that, it makes my character jumps very gently and then immediately fall back to the ground, which is very unexpected.
I heard some people say that I should never use add force in the update method, and that's why I made this flag, but now I'm hearing people say that you can't do that as well, because the update and fixed update method doesn't always match, so Unity could end up skipping my call.
I'm really confused with all that stuff, so I would love if some experienced programmer could tell how to properly handle physics in general in Unity, I really want to make quality games by using the correct approaches, but I'm having a hard time finding more "definitive" solution.
Here's the super simple code I mentioned:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleJump : MonoBehaviour
{
bool jumpNow;
Rigidbody rb;
Vector3 jumpForce = new Vector3(0, 600, 0);
private void Awake()
{
rb = gameObject.GetComponent<Rigidbody>();
}
private void Update()
{
if(Input.GetButtonDown("Jump"))
{
jumpNow = true;
}
}
private void FixedUpdate()
{
if(jumpNow == true)
{
jumpNow = false;
rb.velocity = Vector3.zero;
rb.AddForce(jumpForce * Time.fixedDeltaTime, ForceMode.Impulse);
}
}
}
Thanks in advance for any help! I really appreciate it.
Your answer
Follow this Question
Related Questions
Rigidbody Jumping issues 0 Answers
GetAxis being missed in FixedUpdate work around? 1 Answer
How i make a ball jump where i point with my mouse?[3D] 0 Answers
Jump landing jittering 0 Answers
Storing instantaneous velocity 0 Answers