Hello everyone. I have a code that works well for me. But I’m having trouble making my character just jump once while the button is pressed. Current mode the character jump repeatedly while the button is pressed and I don’t want that. Can anyone help?
private void UpdateVelocity() {
Vector3 velocity = rigidbody.velocity;
if (grounded) velocity = Util.ProjectOntoPlane(velocity, transform.up);
if (grounded) {
// Apply a force that attempts to reach our target velocity
Vector3 velocityChange = (desiredVelocity - velocity);
if (velocityChange.magnitude > maxVelocityChange) {
velocityChange = velocityChange.normalized * maxVelocityChange;
}
rigidbody.AddForce(velocityChange, ForceMode.Impulse);
if (Input.GetButton("Jump")) {
rigidbody.AddForce(Vector3.up*jumpHeight, ForceMode.Impulse);
transform.localEulerAngles = new Vector3(0 , transform.localEulerAngles.y, transform.localEulerAngles.z);
}
}
rigidbody.AddForce(transform.up * -gravity * rigidbody.mass);
grounded = false;
}