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 rompar95 · Sep 09, 2016 at 10:44 AM · c#scripting problemjumprunnerdouble jump

Double jump. Jumping after falling off a platform

I have tried to make a double jumping feature in my game. It works fine. But. A player can jump after leaving a platform if a jump is done. If he uses double jump before he lands, he can't jump after leaving a platform. In my game I don't want a Player to be able to jump if he falls off a platform.

using UnityEngine; using System.Collections;

 public class PlayerController : MonoBehaviour {
 
     public float moveSpeed;
     public float jumpForce;
     public bool grounded;
     public LayerMask whatIsGround;
 
     private Rigidbody2D rb;
     private Collider2D coll;
     private Animator anim;
     private bool canDoubleJump;
 
 
 
     void Start () 
     {
         rb = GetComponent<Rigidbody2D> ();
         coll = GetComponent<Collider2D> ();
         anim = GetComponent<Animator> ();
     }
     
 
     void FixedUpdate () 
     {
         grounded = Physics2D.IsTouchingLayers (coll, whatIsGround);
     }
 
     void Update ()
     {
         rb.velocity = new Vector2 (moveSpeed, rb.velocity.y);
         if (Input.GetButtonDown ("Fire1")) 
         {
             if (grounded) 
             {
                 rb.velocity = new Vector2 (rb.velocity.x, jumpForce);
                 canDoubleJump = true;
 
             }
                 
             if (!grounded && canDoubleJump)
             {        
                 rb.velocity = new Vector2 (rb.velocity.x, jumpForce);
                 canDoubleJump = false;
             }
         }
 
         anim.SetFloat ("Speed", rb.velocity.x);
         anim.SetBool ("Grounded", grounded);
     }
 
     
 }

My guess is to somehow reset a "canDoubleJump" after a Player lands but everything I tried didn't work. So. I need some help. I am a begginer in scripting and Unity as you can obviously see. And my English is not that well) Thanks!

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

Answer by ArturoSR · Sep 09, 2016 at 12:12 PM

Hello there.

OK, you can make the change when the player lands into the ground, he can jump after that, now this is the trick:

  1. If the character it has "touched" any ground layered as "environment" he can make a double jump.

  2. But, if the character it has "touched" any ground layered as "platforms" then he can't make any jump.

So, what you already need to do it is store the layer value to make a comparison at the jump event, something like this:

 if (grounded) {
     if ("The current ground equal to environment") {
         "Then he can jump and double jump";
     }
 } else {
     "Here makes the character go down to ground";
 }

As you can see, only, if the character is the ground established to make a jump he then can jump, but, if not, then he deserves to die (uuuuooooh! you are so bad), cheers.

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 rompar95 · Sep 09, 2016 at 07:59 PM 0
Share

Thanks a lot for the answer, but I did't really understand you. Or, maybe, you didn't understand me) I think you wrote about two types of "ground"/"platforms"/"enviroment". Like I have "Platform_1" where a Player can jump/double jump, and "Platform_2" where he cannot. That's not my case. I have only one type of platforms and they are layered "ground". I repeat, maybe I didn't understand you correctly) If so, then I'll continue thinking of how to use your answer.

Another situation is if YOU didn't understand me. Well, my fault. It is hard to explain, so I'll just show you) ![alt text][1] [1]: /storage/temp/77935-12121.png

We have 4 positions. 1. A player is running. 2. A player is jumping to reach the second platform. If a player doesn't use double jump then he lands on a platform and "CanDoubleJump" is true. So when he runs off the second platform (position 4), he can use his double jump and saves himself. I don't want to have this feature) It may not be bad, but in my game it is like a cheat for me. 2a. A player makes double jump. If he used double jump then "CanDoubleJump" is false. So when he runs off the second platform he is doomed.

Any ideas?

12121.png (24.7 kB)
avatar image ArturoSR rompar95 · Sep 09, 2016 at 10:00 PM 0
Share

O$$anonymous$$, another option will be to check if the player is jumping while is grounded and not falling (need to set a field/variable that allow you to make this comparison, this is because the double jump option).

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

68 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

make rigidbody jump and maintain velocity 1 Answer

Jumping to platform of different depth in 3D game 0 Answers

What am i doing wrong with my jump attack ? 0 Answers

How to double jump? i just can jump once when it touch the ground.. help me guys =D 1 Answer

How can i combine Coyote Time and Jump Buffering with double jump? 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