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 kitommy · Oct 05, 2015 at 10:53 AM · home

Double Jump not working

@Komak57

that's my code to handle double jump.But sometimes it doesn't work.

First time I touch the screen my character jump up ( variable doublejump = true ) .when I touch the screen a second time my character jump higher(double jump = false)(double jump worked).

But sometimes sometimes I touch the screen for the first time(no need to touch the screen a second time) doublejump variable = false(double jump incorrect work as I expected)

Help me fix that

   if (isGrounded) {
                     doubleJump = false;
                 } 
     
                 if (Input.GetMouseButtonDown (0) && this.gameObject.name == "P0") {
                     if (isGrounded) {
                         rid.velocity = new Vector3 (0, 50f * jump, 0);
                         doubleJump = true;
                     } else {
                         if (doubleJump) {
                             rid.velocity = new Vector3 (0, 60f * jump, 0);
                             doubleJump = false;
                         }
                     }
                 }
Comment
Add comment · Show 2
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 Pharan · Oct 05, 2015 at 11:50 AM 0
Share

(1) Why did you need to check the name of the gameObject?

(2) Is it possible that doubleJump is being set to false elsewhere?

(3) Where is this code? Is it inside Update? FixedUpdate? a coroutine?

avatar image kitommy Pharan · Oct 07, 2015 at 01:27 AM 0
Share

because I have a group of objects including a leader(P0) and member of group (P1, P2 ... Pn), I add scripts for all the objects included in the group.When P0 jumped, members also turn jumping $$anonymous$$y code inside FixedUpdate

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Komak57 · Oct 05, 2015 at 05:00 PM

Alright, so first up, let me run through this to make sure I know what's going on. You have a 3D game and you're using a rigidbody as your player. You want to attach this script to multiple objects that you may take control of at various times. You want to be able to jump pretty high on the first try, and then a little higher on the second, and for some reason, this script doesn't work every time (more of an issue with the 2nd jump).

Right on line 2 you're setting doublejump to false. The only instance that should set the double jump, is when you jump for the second time, or when you are about to jump. Remove the first 3 lines and everything should work as expected. My assumption here, is that you're hitting a frame after the first jump that you're still classified as grounded, and your jump is being removed.

As far as the gameObject.name goes, never rely on names unless it's a specific object you want to manage, meaning there's only ever going to be one of that name (never spawning more). Otherwise, I suggest you use tags or layers. In the case that you have multiple units that gain or lose control, I suggest you JumpScript.setEnabled(false) when you start, and set to true (from another script) when possessing.

Comment
Add comment · Show 2 · 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 kitommy · Oct 07, 2015 at 01:28 AM 0
Share

I $$anonymous$$ake Endless 2D game like zombie Tsunami I have a group of objects including a leader(P0) and member of group (P1, P2 ... Pn), I add scripts for all the objects included in the group.When P0 jumped, members also turn jumping All $$anonymous$$y Code:

     if (isGrounded) {
             doubleJump = false;
             jumpAnim = false;

             if (gm.playerState == Game$$anonymous$$anager.PlayerState.normalState) {
                 anim.SetBool ("doubleJump", false);
             } else if (gm.playerState == Game$$anonymous$$anager.PlayerState.$$anonymous$$idas$$anonymous$$ingState) {
                 anim.SetBool ("midasDoubleJump", false);
             } else if (gm.playerState == Game$$anonymous$$anager.PlayerState.NinjaState) {
                 anim.SetBool ("ninjaDoubleJump", false);
             }  
         } else {
             jumpAnim = true;
         }

         if (target == null) {
             if (isGrounded) {
                 if (Input.Get$$anonymous$$ouseButtonDown (0)) {
                     rid.velocity = new Vector3 (0, 50f * jump, 0);
                     doubleJump = true;
                 }
             } else {
                 if (doubleJump) {
                     if (Input.Get$$anonymous$$ouseButtonDown (0)) {
                         rid.velocity = new Vector3 (0, 60f * jump, 0);
                 
                         if (gm.playerState == Game$$anonymous$$anager.PlayerState.normalState) {
                             anim.SetBool ("doubleJump", true);
                         } else if (gm.playerState == Game$$anonymous$$anager.PlayerState.$$anonymous$$idas$$anonymous$$ingState) {
                             anim.SetBool ("midasDoubleJump", true);
                         } else if (gm.playerState == Game$$anonymous$$anager.PlayerState.NinjaState) {
                             anim.SetBool ("ninjaDoubleJump", true);
                         }
                         doubleJump = false;
                     }
                 }
             }
         }

         if (target != null && isGrounded && target.GetComponent<$$anonymous$$ygroup> ().doubleJump && !target.GetComponent<$$anonymous$$ygroup> ().isGrounded && dir <= 2f) {
             StartCoroutine (Jump ());
         }
         if (target != null && !isGrounded && doubleJump && !target.GetComponent<$$anonymous$$ygroup> ().isGrounded
             && !target.GetComponent<$$anonymous$$ygroup> ().doubleJump && dir <= 2f) {
             StartCoroutine (JumpDouble ());
         }
avatar image Komak57 · Oct 07, 2015 at 03:59 AM 1
Share

Again, stop setting doubleJump = false; on line 2. doubleJump should ONLY be set to true when grounded, and set to false when jumping while airborn.

     if (isGrounded) {
         jumpAnim = false; // stop jump animation
     }
 
     if (target == null) {
         if (isGrounded) {
             if (Input.Get$$anonymous$$ouseButtonDown (0)) {
                 rid.velocity = new Vector3 (0, 50f * jump, 0);
                 doubleJump = true; // reset double jump
                 jumpAnim = true; // animate
             }
         } else {
             if (doubleJump) {
                 if (Input.Get$$anonymous$$ouseButtonDown (0)) {
                     rid.velocity = new Vector3 (0, 60f * jump, 0);
                     doubleJump = false; // consume double jump
                 }
             }
         }
     }
     // evaluate animation changes EVERY update, not just when the event occurs
     if (gm.playerState == Game$$anonymous$$anager.PlayerState.normalState) {
         anim.SetBool ("doubleJump", doubleJump && !isGrounded); // evaluate to is double jumping
     } else if (gm.playerState == Game$$anonymous$$anager.PlayerState.$$anonymous$$idas$$anonymous$$ingState) {
         anim.SetBool ("midasDoubleJump", false); // TODO: evaluate midas double jumping
     } else if (gm.playerState == Game$$anonymous$$anager.PlayerState.NinjaState) {
         anim.SetBool ("ninjaDoubleJump", false); // TODO: evaluate ninja double jumping
     }  
 
     if (target != null && isGrounded && target.GetComponent<$$anonymous$$ygroup> ().doubleJump && !target.GetComponent<$$anonymous$$ygroup> ().isGrounded && dir <= 2f) {
         StartCoroutine (Jump ());
     }
     if (target != null && !isGrounded && doubleJump && !target.GetComponent<$$anonymous$$ygroup> ().isGrounded
         && !target.GetComponent<$$anonymous$$ygroup> ().doubleJump && dir <= 2f) {
         StartCoroutine (JumpDouble ());
     }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Hosting a server at home 4 Answers

How can you detect the oculus home overlay without the OVR manager? 0 Answers

[Solved]handle android hardware buttons 1 Answer

How to play Unity on Android's background Home Screen? 1 Answer

HTC, and other Androids with sense UI reloads sense UI when the home button is hit 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