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;
}
}