Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Mirakulous · Jul 21, 2014 at 01:58 PM · c#double jump

restrict number of double jumps

Hi, i am trying to set a limit of doubles jumps available to the player. so for this code i am using i have set the limit to 5 and every time the player double jumps it is reduced by one. so that when it reaches zero then cannot use double jump anymore. however it seems to have back fired and allows the player to continuously jump in the air, even when not on the ground. please help. using UnityEngine; using System.Collections;

public class CharacterMove : MonoBehaviour {

 public float speed = 6.0f;
 Transform groundCheck;
 private float overlapRadius = 0.2f;
 public LayerMask whatIsGround;
 private bool grounded = false;
 private bool jump = false;
 public float jumpForce = 700f;
 private bool doubleJump = false;
 public int dJumpLimit = 5;


 void Awake()
 {
     rigidbody2D.fixedAngle = true;
 }

 void Start()
 {
     groundCheck = transform.Find ("groundcheck");
     PlayerPrefs.SetInt ("doublejumps", dJumpLimit);
 }

 void Update()
 {
     //if (Input.GetKeyDown (KeyCode.Space)){
     //                jump = true;
     //}    
     if (Input.GetMouseButtonDown (0)) 
     {
         jump = true;
     }
 }

 void FixedUpdate()
 {
     //check if character is grounded
     grounded = Physics2D.OverlapCircle (groundCheck.position, overlapRadius, whatIsGround);

     //reset doublejump when player is on the ground
     if (grounded)
         doubleJump = false;

     //determine if player can jump
     bool canJump = (grounded || !doubleJump);

     if (jump && canJump) 
     {
         rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,0);
         rigidbody2D.AddForce(new Vector2(0, jumpForce));
         if(!grounded && dJumpLimit >= 1)
         {
             doubleJump = true;
             dJumpLimit--;
         }
         else if(!grounded && dJumpLimit <= 0)
         {
             doubleJump = false;
         }
     }

     jump = false;

     //apply forward movement
     rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);

 }

}

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 Bojidar · Jul 21, 2014 at 02:32 PM 0
Share

Is GroundCheck parented to your GameObject(e.g. character)? If it is not, it will stay on the groun and OverLap circle will always return true. Also, you are not restoring dJumpLimit back to 5, thus your character will be able da 5-jump only once.

1 Reply

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

Answer by David-Berger · Jul 21, 2014 at 05:39 PM

In the case you have set up your GameObject properly according to the GroundCheck you possible can adjust your code to support proper change of the doubleJump flag.

       [...]

         //check if character is grounded
         grounded = Physics2D.OverlapCircle (groundCheck.position, overlapRadius,whatIsGround);
         
         //reset doublejump when player is on the ground
         if (grounded)
             doubleJump = false;
 
         //deactivate double jump when no jumps are available
         if (dJumpLimit < 1)
             doubleJump = true;
 
         //determine if player can jump
         bool canJump = (grounded || !doubleJump);
 
         if (jump && canJump) 
         {
             rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x,0);
             rigidbody2D.AddForce(new Vector2(0, jumpForce));
 
             if(!doubleJump && !grounded) {
                 doubleJump = true;
                 dJumpLimit--;
             }
         }
         
         jump = false;

[...]

Hope this helps.

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 Mirakulous · Jul 22, 2014 at 08:38 AM 1
Share

thanks so much this worked exactly how i wanted. it won't let me vote your answer as i don't have a rep level of 15 yet.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

How can I edit this script to double jump? 0 Answers

An OS design issue: File types associated with their appropriate programs 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