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 /
avatar image
0
Question by $$anonymous$$ · Jul 07, 2016 at 07:53 AM · c#double jump

Double Jumping in C#

Hey, I know this is posted a lot, but I'm having a problem with my double-jump script. Is anyone able to help?

As it stands, the character sprite simply moves vertically without having an actual double jump.

Note: there are a lot of variables because I intend to allow quad-jumping eventually.

Thanks in advance!

     public int maxjumps = 2;
 int jumps;
 public float movespeed = 2.0f;
 private Rigidbody2D Player_Collider;
 public bool jump1;
 public bool jump2 = true;
 public bool jump3 = true;
 public bool jump4 = true;

 void Awake()
 {
     jumps = maxjumps;
 }

 void Update ()
 {
     if(Input.GetKey(KeyCode.D))
     {
         transform.Translate (new Vector3 (1, 0) * movespeed * Time.deltaTime);
     }
     if(Input.GetKey(KeyCode.A))
     {
         transform.Translate (new Vector3 (-1, 0) * movespeed * Time.deltaTime);
     }
     if(Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.W))
     {
         Jump ();
     }
 }
     void Jump()
 {
     if (jumps == 0)
     {
         return;
     }
     if (jump1 == false)
     {
         GoUp ();
         jump1 = true;
         jump2 = false;
     }
     if (jump2 == false)
     {
         GoUp ();
         jump2 = true;
         jump3 = false;
     }
     if (jump3 == false)
     {
         GoUp ();
         jump3 = true;
         jump4 = false;
     }
     if (jump4 == false)
     {
         GoUp ();
         jump4 = true;
     }
 }
     
 void GoUp()
 {
     GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, 5), ForceMode2D.Impulse);
     jumps = jumps - 1;
 }

 void OnCollisionEnter2D(Collision2D collide)
 {
     if(collide.gameObject.tag == "Ground")
     {
         jump1 = false;
         jump2 = true;
         jump3 = true;
         jump4 = true;
     }
 }

}

Comment
Add comment · Show 1
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 NoseKills · Jul 07, 2016 at 08:05 AM 1
Share

There's a few problems here.

Firstly the way you have arranged your 'if's, if you go into the jump1 if, you set jump2 to false and that makes you enter the next if... that goes for all the jumpX ifs so you always enter all of them or none of them.

Secondly I don't see a reason to use those jump1,2,3,4 variables at all. You already have the 'jumps' and 'maxJumps' variables that holds all the info you need about jumping: how many you have done and how many you can do. You can jump if 'jumps > 0' and when you land you reset jumps to 'maxJumps'.

2 Replies

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

Answer by $$anonymous$$ · Jul 07, 2016 at 05:53 PM

@NoseKills I want to thank you and apologize. I'd figured out how to code it correctly (which will be below for any who need help with this), but I couldn't delete the question. So, thank you for your help, and I'm sorry for the trouble.

Here's the new code:

 public int maxjumps = 2;
 int jumps;
 float jumpforce = 5f;

 void Update()
 {
     if(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W))
     {
         Jump ();
     }
 }
     void Jump()
 {
     if (jumps > 0)
     {
         gameObject.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, jumpforce), ForceMode2D.Impulse);
         grounded = false;
         jumps = jumps - 1;
     }
     if (jumps == 0)
     {
         return;
     }
 }

 void OnCollisionEnter2D(Collision2D collide)
 {
     if(collide.gameObject.tag == "Ground")
     {
         jumps = maxjumps;
         grounded = true;
         movespeed = 2.0F;
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 NoseKills · Jul 07, 2016 at 08:13 PM 0
Share

No problem dude :)

avatar image
0

Answer by Petch12 · Jul 07, 2016 at 05:53 PM

By quad I presume you mean 4 jumps (?) It doesn't really matter though how many times you want to jump. I would set 2 integers for the jumps (both public):

 public int maxAmountOfJumps = 2;
             public int Jumped = 0;
             void Awake()
             {
                 //Not sure why you need this to be honest.
             }
 
 
             void Update()
             {
                 
                 if (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.W))
                 {
                     Jump();
                 }
             }
             
             //Now add Jump function
             
             void Jump()
             {
                 if (Jumped <= maxAmountOfJumps)
                 {
                     GoUp();
                 }
                 else
                 {
                     return;
                 }
             }
 
             void GoUp()
             {
                 //Insert Jump Code.
                 Jumped +=1;
             }
 
             void OnCollisionEnter2D(Collision2D collide)
             {
                 
                 if (collide.gameObject.tag == "Ground")
                 {
                    Jumped = 0; 
                 }
             }

So every time you land you reset Jumped to 0 that way you can only jump 2 times and no more.

Hopefully if this doesn't fix your problem it will at least give you some ideas you can work with.

EDIT: Sorry by the way I made this script in visual studio not via Unity so copying and pasting might not work very well.

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 abesycho · Oct 13, 2018 at 09:05 AM 0
Share

I can't seem to jump anymore after the first double jump dan land on the ground.

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Double jump 2D Platformer 1 Answer

How can I make my character double Jump? (C#) 4 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