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 chesterhilly · May 08, 2015 at 03:04 PM · jumpjumpingball

How to stop multiple mid-air jumps on a gameObject?

Hello, I'm not new to unity, but really can't be bothered to fully learn javascript. I do know bits about javascript but need help on this one particular script. I edited it and it looks like this...

 #pragma strict
 
 var rotationSpeed = 100;
 var jumpHeight = 8;
 var groundHeight = 0;
 
 private var isFalling = false;
 
 
 function Update ()
 {
     //Handle ball rotation.
     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
     rotation *= Time.deltaTime;
     GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
 }
 
 function OnMouseDown () 
 {
     GetComponent.<Rigidbody>().velocity.y = jumpHeight; 
     {    
     isFalling = true;
     }
 }
 
 function OnMouseUp () 
 {
     GetComponent.<Rigidbody>().velocity.y = groundHeight; 
     {    
     isFalling = false;
     }
 }
 
 function OnCollisionStay ()
 {
     isFalling = false;
 }

So what it does is it makes this ball jump up and down. However you can make it jump whilst it is in mid-air as much as you like. So yes, I can make a flappy bird style game but I want it to only jump after it's hit the floor so you can't double jump.

thanks for help

Comment
Add comment · Show 6
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 Dibbie · May 08, 2015 at 03:15 PM 0
Share

I think your problem is HOW you have it set up, you have the right idea with using a contorting boolean variable like "isGrounded" or in your case "isFalling", but you need to make sure that they will only be allowed to jump depending on the result of that boolean.

 JAVASCRIPT:
 
 function On$$anonymous$$ouseDown () 
  {
 if(isFalling == false){
      GetComponent.<Rigidbody>().velocity.y = jumpHeight;
      isFalling = true;
 } 
      
  }
 
 function On$$anonymous$$ouseUp () 
  {
      GetComponent.<Rigidbody>().velocity.y = groundHeight; 
      //isFalling = false;
 // ^ You dont need to set isFalling when they click up, otherwise they can jump again, let the ground deal with this variable now.
  }


You could also do a "jump count" int variable if you want, every time they click down, jumpcount += 1, and then if the jumpcount is less than 1, then allow them to jump, else, do nothing, then when they collide with the ground, set jumpcount back to 0.

avatar image chesterhilly · May 08, 2015 at 03:27 PM 0
Share

Nope, yopu can still do multiple jumps in the air.

heres what the script looks like:

(I removed the update function because its not needed)

 #pragma strict
  
  var rotationSpeed = 100;
  var jumpHeight = 8;
  var groundHeight = 0;
  
  private var isFalling = false;
 
  
 function On$$anonymous$$ouseDown () 
   {
  if(isFalling == false){
       GetComponent.<Rigidbody>().velocity.y = jumpHeight;
       isFalling = true;
  } 
       
   }
  
  function On$$anonymous$$ouseUp () 
   {
       GetComponent.<Rigidbody>().velocity.y = groundHeight; 
       //isFalling = false;
  // ^ You dont need to set isFalling when they click up, otherwise they can jump again, let the ground deal with this variable now.
   }
  
  function OnCollisionStay ()
  {
      isFalling = false;
  }

avatar image fighder · May 08, 2015 at 03:34 PM 0
Share

Is there any other colliders in the air that might've collide with the ball?

Also, why do you need to change velocity.y = groundHeight for On$$anonymous$$ouseUp?

avatar image chesterhilly · May 08, 2015 at 03:41 PM 0
Share

+fighder I used velocity.y = groundHeight for On$$anonymous$$ouseUp because when you let go the ball instantly starts co$$anonymous$$g towards the ground again

avatar image Dibbie · May 08, 2015 at 03:45 PM 0
Share

Hmm... Taking a second look at it, try moving the groundHeight in On$$anonymous$$ouseUp into OnCollisionStay, that way nothing is affected on release, only on press. Logically, to me, thatd make only 1 way the player can jump.

In addition, you could also make an if statement to check the tag of the collided object to ensure its a ground or something thad stop jumping intentionally, just to avoid any outside elements causing your script to mess up.

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by chesterhilly · May 09, 2015 at 12:18 AM

I think I may just have to abandon this and give the ball a constant bounce and accelerometer input.

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 fighder · May 09, 2015 at 12:25 AM 0
Share

I think you should really fix this problem, cuz it's a really basic concept. If you can't fix this, then you will have a lot of trouble when you want to create more complex games.

Normally this is how I will do it:

When click, add force upwards (with formode impulse)

The condition for the jump to occur will be when ball is colliding with anything tagged with "ground".

avatar image
0

Answer by Abdou23 · May 09, 2015 at 09:15 AM

The problem is you instantly changing the bool to true after adding the velocity, so it became true whilst still in the air. To fix that try changing it to true when the ball collides with the ground or something. or when Y position == certain value.

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 ninjared4 · May 09, 2015 at 03:15 PM

Add a Character Controller and do something like this (C#)

 using UnityEngine;
 using System.Collections;
 
 public class SCRIPTNAMEHERE : MonoBehaviour {
 
     public CharacterController cc; //This is set in the inspector window
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (cc.isGrounded && Input.GetButtonDown ("Jump")) {
             //Jump Stuff Here
         }
     }
 }

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 yarodev · Oct 21, 2016 at 07:28 AM

What about 2D?

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

7 People are following this question.

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

Related Questions

Strange Jumping Error? 1 Answer

Jumping Ball 1 Answer

Can't get character to Jump on the Y Axis (C#) 2 Answers

Triple Jump C# Script 1 Answer

2d Jumping Problem 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