- Home /
Jump Further When Key Is Held Down
I'm trying to make my player jump further if they hold down the W key. However, it appears that my player will always jump the same distance. I've been trying to figure this out for a rather long period of time now, and have not been able to find an answer yet. Thank you much for any help!
using UnityEngine;
using System.Collections;
public class JumpInC : MonoBehaviour {
private float distToGround;
//Height of jump
public float jumpHeight;
//To allow fo slopes
public float skin;
//How fast player is forced down.
public float jumpDown;
public bool jumping = false;
void Start(){
// get the distance to ground
distToGround = collider.bounds.extents.y;
}
//Is the player grounded?
bool IsGrounded() {
return Physics.Raycast(transform.position, -Vector3.up, distToGround + skin);
}
void FixedUpdate () {
//Jump
if (Input.GetKey(KeyCode.W) && IsGrounded()){
rigidbody.AddForce(new Vector3(0, jumpHeight , 0),ForceMode.Impulse);
jumping = true;
}
//Jump Further if Held Down?
if (Input.GetKey(KeyCode.W) && jumping == true){
rigidbody.AddForce(new Vector3(0, 1.5f * jumpHeight , 0),ForceMode.Impulse);
}
//If W is released, jump shorter distance
if (Input.GetKeyUp (KeyCode.W) && jumping == true || IsGrounded ()) {
jumping = false;
}
//Push player downwards
if (Input.GetKey (KeyCode.S) && IsGrounded() == false) {
rigidbody.AddForce(0, -jumpHeight * jumpDown, 0);
jumping = false;
}
}
}
Have you tried using Debug.Log to Debug each if statement to see what is actually happening? So in each if Debug.Log the value of jumping, and also use Debug.Log to check if the if statement is actually being hit.
I really need to get in the habit of using Debug.Log... It was the || IsGrounded() on line 46... However, my object is now overco$$anonymous$$g gravity unless my values are quite low. Anyways, the original question posed is solved. Thanks $$anonymous$$eltdown : ).
@PainoHandsThePerson - I'd just come back to post the answer when I noticed you'd figure it out. You should explain your solution a bit more as an answer and then mark your question as answered.
Did so just now. I feel odd accepting my answer as correct for my question when it was due to the input of another person in the community >_<.
@PianoHandsThePerson - it can be a fine line. I'd say the comment headed you down a successful path, but was not the answer. When you get a bit more $$anonymous$$arma, giving them a thumbs up is the right thing to do.
Answer by Aggrojag · Apr 14, 2014 at 12:53 AM
The object can now overcome gravity, however the question is resolved. it was due to || is Grounded() on line 46.
using UnityEngine;
using System.Collections;
public class JumpInC : MonoBehaviour {
private float distToGround;
//Height of jump
public float jumpHeight;
//To allow fo slopes
public float skin;
//How fast player is forced down.
public float jumpDown;
public bool jumping = false;
void Start(){
// get the distance to ground
distToGround = collider.bounds.extents.y;
}
//Is the player grounded?
bool IsGrounded() {
return Physics.Raycast(transform.position, -Vector3.up, distToGround + skin);
}
void FixedUpdate () {
//Jump
if (Input.GetKey(KeyCode.W) && IsGrounded()){
rigidbody.AddForce(new Vector3(0, jumpHeight , 0),ForceMode.Impulse);
jumping = true;
}
//Jump Further if Held Down?
if (Input.GetKey(KeyCode.W) && jumping == true){
rigidbody.AddForce(new Vector3(0, 1.5f * jumpHeight , 0),ForceMode.Impulse);
}
//If W is released, jump shorter distance
if (Input.GetKeyUp (KeyCode.W) && jumping == true) {
jumping = false;
}
//Push player downwards
if (Input.GetKey (KeyCode.S) && IsGrounded() == false) {
rigidbody.AddForce(0, -jumpHeight * jumpDown, 0);
jumping = false;
}
}
}
Your answer
Follow this Question
Related Questions
Jumping not allways registering fix? 0 Answers
Problem between my player jump and player movement 1 Answer
How to make the player turn in the air 1 Answer
Jumping while moving 1 Answer