• 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
-1
Question by kasazwix · Nov 25, 2014 at 08:26 PM · playerjumpjavavarfast

The player ball must jump immediately -

I have created a ball game where the player ball must necessarily jump immediately , but with this script the ball after the first jump , jump only after 4 seconds , i need that the player ball must jump immediately , how can I fix this script?


 #pragma strict
 var jumpObject : Rigidbody;
 var jumpDelay : boolean;
 var canJump : boolean;
 var groundTag : String = "Ground";
 var jumpHeight = 10.0;
 var doublejumpHeight = 7.5;
 private var doubleJump : boolean;
 
 function OnCollisionEnter(col: Collision) {
 if (col.gameObject.tag == groundTag) {
 doubleJump = false;
 canJump = true;
 }
 }
 
 function OnCollisionExit(col: Collision) {
 if (col.gameObject.tag == groundTag) {
 canJump = false;
 }
 }
 
 function Update () {
 if(canJump == true && Input.GetKeyDown(KeyCode.Space)) {
 jumpObject.velocity.y = jumpHeight;
 doubleJump = true;
 }
 if(canJump == false && doubleJump == true && Input.GetKeyDown(KeyCode.Space)) {
 jumpObject.velocity.y = doublejumpHeight;
 doubleJump = false;
 }
 }
 
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 YoungDeveloper · Nov 25, 2014 at 09:45 PM 1
Share

Format your code.

avatar image f4bo · Nov 26, 2014 at 10:10 AM 0
Share

I just tried your code and worked perfectly - So I'm asking you: did you attached that script to the ball or what? I'm asking because I can't find a reason to assign the ball object to a variable jumpObject if the script it is supposed to be attached to the ball. Anyhow this is your code cleaned up a little bit:

 #pragma strict
 var jumpDelay : boolean; 
 var jumpHeight = 10.0; 
 var doublejumpHeight = 7.5; 
 private var canJump : boolean; 
 private var groundTag : String = "Ground"; 
 private var doubleJump : boolean; 
 private var jumpObject : Rigidbody; 
 
 function Start()
 {
     jumpObject=this.rigidbody;
 }
 
 function OnCollisionEnter( col : Collision ) { 
     if( col.gameObject.tag == groundTag ) { 
         doubleJump = false; 
         canJump = true; 
     } 
 } 
 function OnCollisionExit( col : Collision ) { 
     if( col.gameObject.tag == groundTag ) { 
         canJump = false; 
     } 
 } 
 function Update( ) { 
     if( canJump == true && Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) { 
         jumpObject.velocity.y = jumpHeight; 
         doubleJump = true; 
     } 
     if( canJump == false && doubleJump == true && Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) { 
         jumpObject.velocity.y = doublejumpHeight; 
         doubleJump = false; 
     } 
 } 
 
 
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by graslany · Nov 26, 2014 at 08:58 AM

Have you tried logging the changes in the value of your canJump variable ? maybe the physics engine makes your ball perform "little bounces" near to the floor that prevent it to be detected as being on the ground ?

And also, while is is not directly related to the question, and if you allow me to make this kind of side comment: the way you name your fields is quite confusing; jumpHeight for example is actually a speed. canJump and doubleJump both have the same apparent semantics of indicating whever the player can (double-)jump but only one of them has a name starting with "can". None of them is commented. I'm not telling you how you should write your own code, but I saw more than once in my own experience that this kind of imprecision leads to big trouble when you try to debug or read again your code weeks or months later.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dash in movement direction, not in forward direction 0 Answers

How do I get my jump animation to work? 1 Answer

Jump Function C# hassle! 0 Answers

time counter in scripting 2 Answers

Jump Script (Always Up) 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