Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DanielBrg · Jan 29, 2016 at 12:35 AM · unity 2djumpendless runnerdoublejump

Player cannot double jump

Hi, i am following a tutorial on youtube and everything is working fine until now, except i can only have one jump with this code, any advice ?

using UnityEngine;

using UnityEngine.UI;

using System.Collections;

public class Player : MonoBehaviour {

 public float power = 500;
 public int jumpHeight = 1000;
 public bool isFalling = false;

 public int Score;
 public Text SCORE;
 public GameObject YOUDIED;

 private int health = 3;
 public GameObject health1;
 public GameObject health2;
 public GameObject health3;

 public int Highscore;

 private bool canDoubleJump;
 private bool jumpOne;
 private bool jumpTwo;

 // Use this for initialization
 void Start () {
     YOUDIED.SetActive(false);
     Highscore = PlayerPrefs.GetInt ("Highscore", 0);
     jumpOne = false;
     jumpTwo = false;
     canDoubleJump = false;
 }
 void Update() {
     if(Input.GetMouseButtonDown(0) && health == 0) {
         Time.timeScale = 1f;
         health = 3;
         Application.LoadLevel(0);
     }
     if (health == 3) {
         health1.SetActive (true);
         health2.SetActive (true);
         health3.SetActive (true);
     }
     if (health == 2) {
         health1.SetActive (true);
         health2.SetActive (true);
         health3.SetActive (false);
     }
     if (health == 1) {
         health1.SetActive (true);
         health2.SetActive (false);
         health3.SetActive (false);
     }
     if (health == 0) {
         health1.SetActive (false);
         health2.SetActive (false);
         health3.SetActive (false);
     }
     if (health <= 0) {
         YOUDIED.SetActive (true);
         Time.timeScale = 0.0f;
     }
     SCORE.text = "Score " + Score;
     transform.Translate(Vector2.right * power * Time.deltaTime);
     if (Input.GetKeyDown(KeyCode.Space) && isFalling == false) {
         jumpOne = true;
         canDoubleJump = true;
         isFalling = true;
     }
     if (Input.GetKeyDown(KeyCode.Space) && isFalling == true && canDoubleJump == true) {
         jumpTwo = true;
         canDoubleJump = false;
     }
 }
 void FixedUpdate() {
     if (jumpOne == true) {
         GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpHeight);
         jumpOne = false;
     }
     if (jumpTwo == true) {
         GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpHeight);
         jumpTwo = false;
     }
 }
 void OnCollisionStay2D(Collision2D coll) {
     if (coll.gameObject.tag == "Ground") {
         isFalling = false;
         canDoubleJump = false;
     }
 }
 public void ScorePlus(int NewScore) {
     Score += NewScore;
     if(Score >= Highscore) {
         Highscore = Score;
         PlayerPrefs.SetInt ("Highscore", Highscore);
     }
 }
 void OnTriggerEnter2D(Collider2D coll) {
     if (coll.gameObject.tag == "Death") {
         health -= 1;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Trigary · Jan 29, 2016 at 04:00 PM

Providing us with the link of the video would help us.

Line 60 and 65 will both be executed if you press "space", because they are separate "if"s. I think this will fix the problem:

 if (Input.GetKeyDown(KeyCode.Space) {
     if (isFalling == false) {
          jumpOne = true;
          canDoubleJump = true;
          isFalling = true;
     } else if (isFalling == true && canDoubleJump == true) {
          jumpTwo = true;
          canDoubleJump = false;
     }
 }

This will check if "space" is pressed, if it is, it will check further into the conditions. It will save you some processing power, too.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DanielBrg · Jan 29, 2016 at 09:40 PM 0
Share

Thanks, now it's working fine, can't believe i didn't tried this before, thank you very much.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

41 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does unity not detect any keys except "a" and "d" 0 Answers

How can I get a player to move in one direction and around it in the same time unity 2D 1 Answer

Unity 2D jumping only up 0 Answers

Double Jump NoT Working 2 Answers

Pause scene when character hit object, then resume it from another scene 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges