Question by
dev_harsh25 · Oct 06, 2017 at 06:16 PM ·
c#scripting problemjumpingjumping objectdouble jump
player not able to double jump
Hello! I made this script for double jumping.But my player does not double jump,but it can jump in the air once if it falls off the ground. here's the script:-
using UnityEngine; using System.Collections;
void Start(){ anim = GetComponent (); rb = GetComponent ();
}
void FixedUpdate ()
{
if (Input.GetKeyDown (KeyCode.LeftControl)) {
anim.SetBool ("attack", true);
} else {
anim.SetBool ("attack", false);
}
if (Input.GetKeyDown (KeyCode.Space) ){
if(IsGrounded){
rb.velocity += new Vector3 (rb.velocity.x,jumpHeight,rb.velocity.z);
dj = true;
}else{
if(dj){
dj = false;
rb.velocity += new Vector3 (rb.velocity.x,jumpSpeed,rb.velocity.z);
}
}
}
float moveH = Input.GetAxis ("Horizontal");
Vector3 movement = new Vector3 (moveH * 20, rb.velocity.y, 0f);
rb.velocity = movement;
if (moveH < 0) {
rb.rotation = Quaternion.Euler (0, 180, 0);
}
if (moveH > 0 && transform.eulerAngles.y == 180) {
rb.rotation = Quaternion.Euler (0, 0, 0);
}
}
void OnCollisionEnter(Collision other){
if (other.gameObject.tag == "Ground") {
IsGrounded = true;
}
}
void OnCollisionExit (Collision other)
{
if (other.gameObject.tag == "Ground") {
IsGrounded = false;
}
}void OnCollisionStay(Collision other){
if (other.gameObject.tag == "Ground") {
IsGrounded = true;
}
}
}
Comment
dj is only declared in if(IsGrounded) so it's not available in the else. You probably just need to declare dj somewhere outside the if/else statements
Have you placed breakpoints in your script and tried debugging that way?