- Home /
Creating a variable jump using deltaTime in C#.
Hey everyone, I'm currently working on a game for my college project (some of you may know from my previous question) and I want to create a variable jump height that is controlled using deltaTime. So the longer the player holds the spacebar down, the higher the jump will be. My current code is below and I am basically stumped from here, any help will be greatly appreciated. Thank you.` private bool Jumping; private bool falling;
// Use this for initialization
void Start () {
Jumping = false;//start off not jumping
Falling = false;//start off not falling
}
// Update is called once per frame
void Update () {
print (transform.position.y);
if (Input.GetKeyDown (KeyCode.Space)) { //if the player presses the Space Bar
if (!Jumping) { //and if you're not already jumping
rigidbody.AddForce(0, 500, 0);//apply an upward force to the runner
Jumping = true;//now you're jumping
}
print ("Falling");
if (!Jumping && transform.position.y>0.5) {//if the player has released the space bar after jumping
Falling = true;//then the player is falling
//if (Input.GetKeyDown (KeyCode.Space) && transform.position.y<1.6){//if jumping and player is higher than 3
//rigidbody.AddForce(0, 300, 0);//apply more upward force
//}
}
}
void OnCollisionEnter (){ //when the player collides with anything
Jumping = false;//then you are not jumping
Falling = false;//then you are no longer falling
}
}`
I have applied a change to this coding, trying to keep it as basic as possible.` public Vector3 jumpVelocity; private bool touchingPlatform;
// Use this for initialization
void Start () {
touchingPlatform = true;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Jump")) { //if the player presses the Space Bar
if (touchingPlatform) { //and if you're touching the platform
rigidbody.AddForce(jumpVelocity, Force$$anonymous$$ode.VelocityChange);//apply an upward force to the runner
}
}
}
void OnCollisionEnter (Collision col){ //when the player collides
if (col.gameObject.name == "cube") {//with a game object called cube
touchingPlatform = true;//then you are touching the platform
}
}
}`
Your answer
Follow this Question
Related Questions
character controller wont rise when I Jump :( 1 Answer
Rigidbody2D control jump height 0 Answers
Touch the screen and jump two time with the same height 1 Answer
How to properly declare this variable 2 Answers
animation to play faster over time 2 Answers