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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by HORRIBLEPANCAKE · Jun 08, 2014 at 07:42 AM · jumpingspacebar

Fixing a jumping script in C#

 enter code hereusing UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     public float maxSpeed = 15;
     public float jumpForce = 800f;
     
     void FixedUpdate () {
 
         this.rigidbody2D.velocity = new Vector2 (  Input.GetAxis("Horizontal") * maxSpeed ,this.rigidbody2D.velocity.y);
 
         if (Input.GetKeyDown (KeyCode.Space)) {
             rigidbody2D.AddForce(new Vector2(0,jumpForce));    
 
         }
     }
 }


So this is my code for jumping with the sprite, but the problem is, once i hit space bar and i hit the space bar again, the sprite will just keeping jumping and jumping until i stop hitting the space bar.

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 Stygtand · Jun 08, 2014 at 09:03 AM

Then you press the jump button, you signal the event "input.getkeydown". This happens in a fixed update. If i recall right, that happens aprox every 0.33 seconds.

Depending on your game, the best way to do resolve this issue, is A. Put a jump count on. This can give your player the ability to double jump or even triple jump, if thats what you want.

B. If you just want your place to jump once. Implement a method to see if unity is grounded.

Heres an example you could use. You propperly need to redefine it.

 bool jumping = false;
 
 void Update()
 {
     if (input.GetKeyDown (KeyCode.Space) && !jumping) // if key down and is not already jumping
     {
         jumping = true;
     }
 }
 
 void FixedUpdate () 
 {
      
     this.rigidbody2D.velocity = new Vector2 ( Input.GetAxis("Horizontal") * maxSpeed ,this.rigidbody2D.velocity.y);
      
     if (jumping ) // if you are allowed to jump continue
     {
         rigidbody2D.AddForce(new Vector2(0,jumpForce));
         jumping = false; // limits jumping
      
     }
 }
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 flaviusxvii · Jun 08, 2014 at 02:26 PM 0
Share

If i recall right, that happens aprox every 0.33 seconds.

You don't recall right. http://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html

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

24 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

Related Questions

hard to explain but, when my game is paused my space bar goes weird. 0 Answers

Need help with sprite animating 1 Answer

I have been trying to get a working isGrounded variable to get my character to stop from infinitely jumping can someone please show and/or explain what it is that I am doing wrong. 1 Answer

Jumping doesn't work, and I don't know why. 1 Answer

Jumping Issue, Need Help. 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