- Home /
ForceMode.Impulse Doesnt always fire.
I'm trying to make it so when jump is pressed move the character forward and up. What i have below works alright until say about the third press or you spam the button sometimes it doesn't not register. Could it be me limiting the speed? Is there a way to fix this? Thanks in advanced.
void FixedUpdate() {
float maxSpeed = 2.0f;
Vector3 vel = rigidbody.velocity;
if(rigidbody.velocity.x > maxSpeed){
vel.x = maxSpeed;
rigidbody.velocity = vel;
}
if(rigidbody.velocity.y > maxSpeed + 1.8f){
vel.y = maxSpeed;
rigidbody.velocity = vel;
}
if (controlLock != true) {
if (Input.GetButtonUp ("Jump")) {
rigidbody.AddForce(new Vector3(3, 14, 0),ForceMode.Impulse);
}
}
}
Answer by gamemakerdude15145 · Aug 29, 2020 at 10:01 PM
Try this for jumping.
Edit: That is for 2d, but it should work if you use Rigidbody instead of Rigidbody2D and Vector3 instead of Vector2.
Answer by tonialatalo · Aug 29, 2020 at 10:33 PM
Input does not work in FixedUpdate. Do the Input.GetButtonUp in Update instead, that way it is 100% reliable.
You can't use FixedUpdate to do input detection such as keypresses. FixedUpdate does not necessarily fire every frame, so input will be lost sometimes.
From: https://answers.unity.com/questions/743221/is-input-detection-ok-to-do-in-fixedupdate.html
Your answer
Follow this Question
Related Questions
Inconsistent Jump height 3d 3 Answers
Rigidbody is Pushing Another Rigidbody : Unity 1 Answer
Manually Apply Cars Collision Response Force 0 Answers