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 axel2021 · Jul 15, 2016 at 03:20 AM · scripting beginnerjumpingball

I Have A Jump Script, But I need to Edit it To Where The Player Is Only Limited To One Jump

I am creating a game in which the player controls a ball. I was able to make a script in which the ball can jump, but if you keeping pressing the spacebar, it will continue to go up. I am not very experienced with C# so I'm not sure how to put a limit on how much the player can jump. After viewing my script, if you think you have a better jump script or know how to limit the jumps, please tell me. Also, if you need to look at my camera and player control (movement) scripts, I will provide those. Here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class Jump : MonoBehaviour
 {
     public float jump;
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             this.GetComponent<Rigidbody>().AddForce(new Vector3(0, jump, 0));
         }
     }
 }
 

 
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

2 Replies

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

Answer by YasinJavaid_ · Jul 15, 2016 at 09:42 AM

hi here is your updated code . just add Tag "ground" to your terrain or ground. using UnityEngine; using System.Collections;

  public class Jump : MonoBehaviour
  {
      public float jump;
      private bool isGround;
      // Use this for initialization
      void Start()
      {
         isGround = true;
      }
  
      // Update is called once per frame
      void Update()
      {
          if (Input.GetKeyDown(KeyCode.Space) && isGround)
          {
              isGround = false;
              this.GetComponent<Rigidbody>().AddForce(new Vector3(0, jump, 0));
          }
      }
      void OnCollisionEnter(Collision collision) {
         if (collision.gameObject.tag == "ground"){
             isGround = true;
         }
      }
  }
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 axel2021 · Jul 17, 2016 at 12:37 AM 0
Share

Thank you!

avatar image
4

Answer by YasinJavaid_ · Jul 15, 2016 at 12:34 PM

hi here is your code and one advice be more learner and exploring unity learn in start. you have to add "Ground" tag to your terrain or any thing that is ground.. .
(Y)

  public class Jump : MonoBehaviour
  {
      public float jump;
      private bool is ground;
      // Use this for initialization
      void Start()
      {
  
      }
  
      // Update is called once per frame
      void Update()
      {
          if (Input.GetKeyDown(KeyCode.Space) && Ground)
          {    
             ground = false;
              this.GetComponent<Rigidbody>().AddForce(new Vector3(0, jump, 0));
          }
      }
     void OnCollisionEnter(Collision collision) {
         if (collision.gameObject.tag == "Ground"){
             ground = 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

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

Why is my raycast projecting from the middle instead of the bottom? 0 Answers

How can I make my jumping only work when I'm touching the ground? 2 Answers

i want to move a ball on z axis and jump on y axis , 2 Answers

Ball rolling across boxes randomly jumping 1 Answer

I'm trying to allow my character to only jump when grounded 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