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 kawales · Dec 26, 2014 at 06:26 PM · c#android2d

Disable jumping more than once

So today I started working on my little android game but I ran into a problem whit my moving script. Whenever I jump I will be able to jump more than once.I have attached the script to the player and all that to a button, TY! Code:

 using UnityEngine; using System.Collections;
 
 public class Move : MonoBehaviour {
 
 // Use this for initialization
 void Start () {
     ;
 }
 
 // Update is called once per frame
 void Update () {
     rigidbody2D.AddForce (Vector2.right * 10);
     }
 
 public void Groundand(Collider2D coll){
     if (coll.gameObject.name == "ground") {
         rigidbody2D.velocity = transform.up * 10;
     }
 
 
 }
 
 }
Comment
Add comment · Show 9
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 Denvery · Dec 26, 2014 at 07:31 PM 0
Share

Where do you call Groundand?

avatar image LucasMars · Dec 26, 2014 at 07:37 PM 2
Share

Couldn't you just have an if statement, like this:

 if(grounded==true){
       jump();
 }
 else{
       //do nothing
 }

Grounded would be a variable that states if the player is grounded (obviously) and jump() would be a void to jump.

avatar image kawales · Dec 26, 2014 at 08:07 PM 0
Share

@Lucas$$anonymous$$ars but how would I check if they are coliding?

avatar image Denvery · Dec 26, 2014 at 08:10 PM 1
Share

Attach colliders to the ground and the enemy. And paste the method OnCollisionEnter() to you script.

avatar image Mmmpies · Dec 26, 2014 at 08:12 PM 0
Share

Or if you don't need a grounded player for your game but you know how long your jump animation takes when you jump (let's call that float JumpLength) set a

bool isJumping = true;

when you start jumping and set a

float JumpStartTime = Time.time;

The in Update()

 Update()
 {
     if(isJumping)
     {
         if(Time.time > JumpStartTime + JumpLength)
             isJumping = false;
     }

Only allow jumping if isJumping = false

Really though you probably do want to know if the player is on the ground.

Of course $$anonymous$$arion/Sonic type games allow air jumping so it depends on how you want your game to function.

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by jpthek9 · Dec 26, 2014 at 10:08 PM

Plug this into your script and it should work. Basically, an integer counts how many jumps you can do and doesn't let you jump if you have none left. Every time you hit the ground, the integer resets and you get another set of jumps.

 int JumpCount = 0;
 public int MaxJumps = 1; //Maximum amount of jumps (i.e. 2 for double jumps)
 void Start()
 {
 JumpCount = MaxJumps
 }
 void Update()
 {
 if (Input.GetButton("Jump"))
 {
 if (JumpCount > 0)
 {
 Jump();
 }
 }
 }
 public void Jump()
 {
 rigidbody2D.velocity = transform.up * 10;
 JumpCount -= 1;
 }
 void OnCollisionEnter2D(Collision2D Col)
 {
 if (Collision.gameObject.tag == "ground")
 {
 JumpCount = MaxJumps;
 }
 }

Comment
Add comment · Show 9 · 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 kawales · Dec 27, 2014 at 09:52 AM 0
Share

Didn't work since the buttons im using are the UI buttons. So when I add everything and change the button name to Jump it still doesn't work.

avatar image Mmmpies · Dec 27, 2014 at 10:16 AM 0
Share

So you created a button in the UI that, when clicked, should make it jump.

Follow @jpthek9 example but remove the lines in Update.

On you're UI (I'm assu$$anonymous$$g 4.6) select the jump button.

Click on AddComponent in the inspector and add an EventTrigger and click on the + just below it. Add OnPointerClick event then drag whatever GameObject the script is attached to into the empty slot that appears.

From the dropdown box to the right select $$anonymous$$ove -> Jump

That's assu$$anonymous$$g your script is still called $$anonymous$$ove.

When you click it should trigger the Jump function.

avatar image jpthek9 · Dec 28, 2014 at 04:38 AM 0
Share

Did I really deserve a down vote? Obviously you just change the method of input. Paste this in place of the update.

  void OnGUI()
  {
    if (GUI.Button(new Rect(10, 10, 50, 50), "Jump")) //$$anonymous$$ake your button here
  {
  if (JumpCount > 0)
  {
  Jump();
  }
  }
  }

And your script doesn't work because OnCollisionEnter2D is what gets called when you have 2d colliders colliding, not GroundCheck.

avatar image kawales · Dec 28, 2014 at 09:31 AM 0
Share

@jpthek9 Stupid question but why would I create a button whit the old GUI since I already made a button whit the new UI?

avatar image Mmmpies · Dec 28, 2014 at 10:24 AM 0
Share

You don't but also @jpthek9 didn't deserve a down vote.

You didn't mention that you were using UI buttons in the original post so that solution would be valid for the original answer given all that was known at the time.

Show more comments
avatar image
1

Answer by kawales · Dec 27, 2014 at 10:16 AM

Yay I found the answer! For all the people that are curious heres the code I made a new script just fro jumping even tho I didn't have to do that :P

Code:

  1. using UnityEngine; using System.Collections;

    public class JumpScript : MonoBehaviour { public int JumpCount = 1; // Use this for initialization void Start () {

          }
             
             // Update is called once per frame
             void Update () {
             
             }
             
             public void GroundCheck(Collider2D coll){
         
                         if (coll.gameObject.name == "ground" && JumpCount == 1) {
                     
                                 rigidbody2D.velocity = transform.up * 10;
                                 JumpCount = JumpCount - 1;
                         } else if (coll.gameObject.name == "ground" &&
     JumpCount == 0) {
                     JumpCount = JumpCount + 1;
                             
                         }
                         
                     }
    
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 kawales · Dec 27, 2014 at 10:50 AM 0
Share

Nvm I now have even more problem cuz when I click jump it jumps normally then when i click the next time he doesn't jump but if i spam it will jump every second time...

avatar image
0
Wiki

Answer by the1337Wolf · Jan 04, 2015 at 06:52 AM

conversely, you could also have a variable public bool isGrounded = true; set this to false in your jump function. Then create a function onCollisionEnter2D and tag the ground sprite with "Ground". In this function, check if the collision is with "Ground", If it is then set isGrounded to true. Only allow the player to jump if isGrounded is True.

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

Answer by mreina · Jun 06, 2020 at 02:40 PM

Hello,

Another possibility is the following:

Create a public method in your character controller script, which will disable the jumping

 // This method is called from an event in the jump animation, when the animation is finished
 public void Land()
 {
     _isJumping = false;
 }    

Go to the Animator in Unity, double click on your jump animation.

Go the Event section

Add an event at the end of the animation timeline:

In the function field enter the nameof the your public function"Land"

In the Object field select your character controller script

I think that this should work even if you land on something that is not the ground.

Regards

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.

Follow this Question

Answers Answers and Comments

33 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

Related Questions

Make it to where pressing a button on screen doesn't shoot a projectile? 1 Answer

Android 2D Character Controller 2 Answers

2D multi touch 1 Answer

After exporting my game stopped working 0 Answers

Color area below line renderer? 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