- Home /
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
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.
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;
}
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?
+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
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.
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.
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".
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.
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
}
}
}
Your answer
Follow this Question
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