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 Gusic · Feb 17, 2014 at 04:09 PM · c#2dsimplebegginer

Simple Jump Script Help

Hi Guys, I have a problem with my very simple jump script. Basicly i can jump, but before i land i can still keep junping. so when i press space before me player lands if i press space again he will keep jumping for ever. Please help me i am new to c#.

Thank You

 using UnityEngine;
 using System.Collections;
 
 public class Jump : MonoBehaviour {
     public float jumpPower = 1;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetButtonDown("Jump")) {
             rigidbody2D.AddForce(transform.up*jumpPower);
         }
     
     }
 }
 
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 Klarax · Feb 17, 2014 at 04:27 PM 0
Share

use a bool to see if jumping

if(jumping) { //dont allow jump } else { //jumping is good }

avatar image Gusic · Feb 17, 2014 at 04:29 PM 0
Share

i dont get what you mean should i change from float to bool? im a begginer

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Pigold11 · Feb 17, 2014 at 04:29 PM

Use a variable to determine if your player is on the ground, and determine if it is true by using OnCollisionStay, like this:

 using UnityEngine;
 using System.Collections;
  
 public class Jump : MonoBehaviour {
     public boolean grounded = true;
     public float jumpPower = 1;
     // Use this for initialization
     void Start () {
  
     }
  
     // Update is called once per frame
     void Update () {
        if (Input.GetButtonDown("Jump") && grounded == true) {
          rigidbody2D.AddForce(transform.up*jumpPower);
        }
  
     }
 }

 void OnCollissionEnter(){
     grounded = true;
 }
 void OnCollissionExit(){
     grounded = false;
 }

and that will work better than checking if your jumping, because if he's falling he isnt jumping but you still shouldnt be able to jump.

Comment
Add comment · Show 8 · 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 Gusic · Feb 17, 2014 at 05:13 PM 0
Share

it didnt work i can still jump more when im in the air

avatar image JudgeDredd · Feb 17, 2014 at 05:16 PM 0
Share

Collision is spelled wrong and you should set grounded to false in the jump if clause.

avatar image poncho · Feb 17, 2014 at 05:31 PM 0
Share

the answer is good, but just disable the grounded in case you jumped

 if (Input.GetButtonDown("Jump") && grounded == true) {
 rigidbody2D.AddForce(transform.up*jumpPower);
 grounded = false;
 }
avatar image Gusic · Feb 17, 2014 at 05:33 PM 0
Share

when the ground is set false the player cant jump.

avatar image Gusic · Feb 17, 2014 at 05:34 PM 0
Share

everything is great thank you. but when i land i cant jump again

Show more comments

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Next Level Script Wont Work 1 Answer

RaycastHit2D not detecting a specific gameobject in a for (for path smoothing in A*) 1 Answer

Restrict 2D camera with a Quad? 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