- Home /
Hold space longer = Longer jumps?
Hey all,
I'm trying to make a game where the jump length is changed based on if the player is holding the space bar or just tapping it.
I honestly don't have an idea of where to start with this, so all help is appreciated.
Thanks
You could add force while the button is down up to a certain duration. Once the duration is surpassed just don't add the force. Reset the button down time whenever the character is on the ground and the space button is depressed.
Answer by fafase · Jun 19, 2013 at 09:47 AM
Let's consider you are doing a 2D platform game. If not, no worry, the principle remains the same.
Her is a class that allows you to do some movement in 2D and the jump has a minimal jump and if you keep on pressing the jump is larger:
public float speed = 6.0F;
public float jumpSpeed = 20.0F;
public float gravity = 20.0F;
public float gravityForce = 3.0f;
public float airTime = 2f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
private float forceY = 0;
private float invertGrav;
void Start(){
// invertGrav is set greater than gravity so that our guy jumps
invertGrav = gravity * airTime;
controller = GetComponent<CharacterController>();
}
void Update() {
moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (controller.isGrounded) {
// we are grounded so forceY is 0
forceY = 0;
// invertGrav is also reset based on the gravity
invertGrav = gravity * airTime;
if (Input.GetKeyDown(KeyCode.Space)){
// we jump
forceY = jumpSpeed;
}
}
// We are now jumping since forceY is not 0
// we add invertGrav to our jumpForce and invertGrav is also
// decreased so that we get a curvy jump
if(Input.GetKey(KeyCode.Space) && forceY != 0 ){
invertGrav -= Time.deltaTime;
forceY += invertGrav*Time.deltaTime;
}
// Here we apply the gravity
forceY -= gravity*Time.deltaTime* gravityForce;
moveDirection.y = forceY;
controller.Move(moveDirection * Time.deltaTime);
}
It might not be the best and it probably needs some modifications but it works...
Works great, thanks a lot. I just separated the jumping from the body of the script and added it to my current move script.
The problem I found with this is that On$$anonymous$$eyUp the player falls abruptly but if he keep the key pressed he falls slowly.
Answer by elhispano · Jun 19, 2013 at 06:50 AM
I recommend you to work with AddForce. The first time you press space, apply an instant force with ForceMode.VelocityChange, this will be the basic jump, and then, while Space button is pressed, continue using AddForce but with ForceMode.Acceleration as parameter so each frame Space key keep pressed, yo apply a force to your character so it will continue going up before start falling.
void Jump ()
{
if (Input.GetKeyDown (KeyCode.Space)) {
rigidbody.AddForce(Vector3.up * 10,ForceMode.VelocityChange);
}
if (Input.GetKey (KeyCode.Space)) {
if (jumpValue < maxJumpValue) {
rigidbody.AddForce(Vector3.up * 10,ForceMode.Acceleration);
}
}
}
good Idea, but you shouldn't use addforce in the update cycle, because its not framerate independent. to solve that quick and dirty multiply the force with time.deltatime to take the changing framerate into account.
Your answer
Follow this Question
Related Questions
Help Me with My Script Pls?????? 1 Answer
How difficult would it be? 1 Answer
Animation Cycle Dilemma 0 Answers
Tilt control for jump 0 Answers
How to make smooth jump? 1 Answer