double jump - max once
Hi, I wrote script for double jumping and I noticed I can jump how many times I want. How can I make double jump once, for just two jumping and that's all? Here's my script (include few lines of moving but don't pay attention on it):
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class Moving : MonoBehaviour {
bool isOnGround;
public Transform groundCheck;
public LayerMask whatIsTheGround;
float groundSphere = 0.3f;
bool isDoubleJumpAvailable;
public float jumpForce;
public float secondJump;
public float jumpForceHold;
public float maxJumpTime;
float MaxJumpTimeInternal;
public Rigidbody rBody;
public float ballSpeed = 10f;
void Start()
{
rBody = GetComponent<Rigidbody>();
}
void Update()
{
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
rBody.AddForce(new Vector3(h, 0, v) * ballSpeed);
if (isOnGround && Input.GetButtonDown("Jump"))
{
GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpForce, 0));
MaxJumpTimeInternal = maxJumpTime;
isDoubleJumpAvailable = true;
}
if (Input.GetButton("Jump") && GetComponent<Rigidbody>().velocity.y > -1 && MaxJumpTimeInternal > 0)
{
GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpForceHold, 0));
MaxJumpTimeInternal = MaxJumpTimeInternal - 1;
}
if (!isOnGround && isDoubleJumpAvailable && Input.GetButtonDown("Jump"))
{
isDoubleJumpAvailable = false;
MaxJumpTimeInternal = maxJumpTime / 2;
GetComponent<Rigidbody>().Sleep();
GetComponent<Rigidbody>().AddForce(new Vector3(0, secondJump, 0));
}
}
void FixedUpdate()
{
isOnGround = Physics.OverlapSphere(groundCheck.position, groundSphere, whatIsTheGround).Length > 0;
}
}
The smoking gun appears to be isOnGround not being set as expected but really throw in a whole heap of Debug.,Log's in there to see exactly what's happening.
Fixed update and Update are not the same thing so setting isOnGround in one and checking isOnGround in the other can cause inconsistencies.
I checked Debug.Log's but nothing special I noticed but maybe I wrote it bad and that's why so if you can please tell me how to exactly write it to check.
I noticed I can't jump again when my jumping object starts falling.
Try setting isOnGround to false as soon you do the first jump. Can't really test it myself as I don't have access to Unity at the moment but ...
if (isOnGround && Input.GetButtonDown("Jump"))
{
GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpForce, 0));
$$anonymous$$axJumpTimeInternal = maxJumpTime;
isDoubleJumpAvailable = true;
isOnGround = false;
}
Answer by Mmmpies · May 25, 2016 at 02:30 PM
Right what's this bit for?
if (Input.GetButton("Jump") && GetComponent<Rigidbody>().velocity.y > -1 && MaxJumpTimeInternal > 0)
{
GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpForceHold, 0));
MaxJumpTimeInternal = MaxJumpTimeInternal - 1;
}
Because it's not checking for isOnGround and it's not checking isDoubleJumpAvailable but it is applying force. As a quick test /astrix before it and astrix/ after it to comment out.
Honestly, I exactly don't know because I'm beginner and all code is copied from a tutorial on youtube but I tried to check this and after deleting this my object is almmost not jumping, exactly it jumps for 0.0x seconds and for few milimetrs. Options are that same as previous so it's something about force of jump, smth like that.