- Home /
Max height jump
Hello everyone, I have a problem in my code and would like your help. The problem is that when the jump button is pressed, the character jumps repeatedly, I want him to jump only once even if the resume button pressed. I've tried a few things but he didn't jump completely. And I would help so that my character has a maximum height of jump and then return to the floor
if (Input.GetButton("Jump")){
rigidbody.AddForce(Vector3.up * jumpHeight,ForceMode.Impulse);
}
else
rigidbody.AddForce(-Vector3.up * jumpDown,ForceMode.Impulse);
http://answers.unity3d.com/questions/919654/how-do-you-make-a-2d-rigidbody-jump-once-in-unity.html would be a good idea to stop jumping while not on the ground. You're also using get button which is true aslong as you hold the button down you most likely want GetButtonDown as it is only true the first frame.
You should only allow jumping when your character is standing on the floor. Also, use GetButtonDown
so it will be easier to add double jumps later
Try this
(Input.Get$$anonymous$$eyDown("space")
Ins$$anonymous$$d of
if (Input.GetButton("Jump"))
I appreciate your answer. But I'm using GetButton ("Jump") because my character can jump with variable height and GetButtonDown wouldn't work well
Answer by d2 · Mar 10, 2015 at 04:44 PM
instead of Input.GetButton("Jump") use Input.GetKeyDown("Jump") or Input.GetKeyUp("Jump")
If I use GetButtonDown the character gives only a tiny jump. I want to make the character jump with variable height, but with a height limit
try something like this:
using UnityEngine;
using System.Collections;
public class Answers : $$anonymous$$onoBehaviour {
public Rigidbody rigidbody;
public float variableForce;
public float jumpLimit;
void FixedUpdate () {
if (Input.GetButton("Jump")){
rigidbody.AddForce(Vector3.up * variableForce,Force$$anonymous$$ode.Impulse);
}
else{
rigidbody.AddForce(-Vector3.up * variableForce,Force$$anonymous$$ode.Impulse);
}
if(transform.position.y >= jumpLimit)
rigidbody.velocity = Vector3.zero;
}
}
Answer by AndrewToth · Mar 10, 2015 at 07:13 PM
You can try this
if(Input.GetButtonDown("Jump"))
{
//do awesome stuff
}
Your answer
Follow this Question
Related Questions
Max height jump 1 Answer
Jump & Animation Script 2D 0 Answers
Android 2D Character Controller 2 Answers
Character doesn't jump repeatedly 1 Answer
GetButtonDown not always firing 1 Answer