• 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 Phanaamana · Apr 28, 2016 at 10:24 PM · scripting problemjumping

Double jumping not working - can't find the culprit

Hello

I'm new to scripting and thus follow a tutorial. My double jumping isn't working, though, and I went through the code in the tutorial again, but couldn't find the culprit. I tried adding a public Rigidbody2D to reference and then set it's velocity on the Y axis to 0, but alas. Here's my entire player script, sorry if it looks a bit weird and confusing, I took the irrelevant parts out:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 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 ("Længste karriere: ", 0);
         jumpOne = false;
         jumpTwo = false;
         canDoubleJump = false;
 
     }
 
     void Update()
     {
         if (Input.GetKey (KeyCode.Space) && health == 0)
         {
             Time.timeScale = 1f;
             health = 3;
             SceneManager.LoadScene (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 = "Dage " + 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;
         }
     }
     
     // Update is called once per frame
     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("Længste karriere: ", Highscore);
         }
     }
 
     void OnTriggerEnter2D (Collider2D coll)
     {
         if (coll.gameObject.tag == "Death")
         {
             health -= 1;
     }
  }
 }

Comment
Add comment · Show 3
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 jgodfrey · Apr 28, 2016 at 11:49 PM 0
Share

There are some problems with the pasted code. For instance...

  • The Update() method looks empty (between lines 35 and 37)

  • There is a lone "x" on line 38

  • The class doesn't have a closing brace

I assume the above is related to cut/paste issues and not your actual code. Please fix that. And, whe you paste the new code, select it all and press the "101010" button in the toolbar to format it properly for the forum.

avatar image Phanaamana · Apr 29, 2016 at 05:51 PM 0
Share

Oh, sorry - I thought it looked weird. Here's my entire code in 10101010:/

[ newly pasted code moved into original question by @jgodfrey ]

I am getting no error messages in Unity, by the way. And thank you for taking the time to answer, @jgodfrey

avatar image jgodfrey · Apr 29, 2016 at 07:16 PM 0
Share

I moved your newly pasted code into the original question (by editing the question) because you pasted the new code as an answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Apr 29, 2016 at 07:50 PM

Do not use Input.GetKeyDown (KeyCode.Space) two times within the Update to see if the button is pressed two times. When someone holds the key down both will be activated

  • check if the key is pressed with Input.GetKeyDown (KeyCode.Space)

  • check if the key is release with input.GetKeyUp(KeyCode.Space) to get ready for the next jump.

  • check the time between release and the next press to see if double jump is allowed.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

63 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 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

Space bar input not recognized 3 Answers

Can't get my character controller to jump properly (3d) 1 Answer

How to get jumping to return to 0,0,0 quicker 0 Answers

Error CS0120 : An object reference is required to access non-static member 3 Answers

Jump Force/Velocity is Wrong and Strange. Help 1 Answer


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