Im using JavaScript as I just started using unity, and im wondering if theres a way to code
If (An action is done 3x within 5 seconds)
Then “Cancel or negate” the third time.
Im trying to do this with a jump, this is my current code
#pragma strict
var rotationSpeed = 100;
var jumpHeight = 8;
var jumpHeight2 = 16;
var doubleJump : boolean = false;
var doubleJumpCount : int = 0;
var ballSound : Transform;
var distToGround : float;
function Start ()
{
// Getting the distance from the center of our ball to the ground.
distToGround = GetComponent.<Collider>().bounds.extents.y;
}
function IsGrounded() : boolean
{
// Making function, we call this to check if we are on the ground.
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.075);
}
function Update ()
{
if (IsGrounded () )
{
doubleJumpCount = 0;
}
if (doubleJumpCount >= 3)
{
doubleJumpCount = 0;
}
//doubleJump = true;
// Making the ball rotation, and controls.
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
//Making us jump, then checking if object is on the ground.
if (Input.GetKeyDown (KeyCode.Space))//. && IsGrounded ())
{
if (doubleJumpCount >= 2)
{
return;
}
else
{
GetComponent.<Rigidbody>().velocity.y = jumpHeight;
doubleJumpCount += 1;
}
var sound = Instantiate (ballSound, transform.position, transform.rotation);
//Here we destroy the variable to clean our hierarchy. "3" is the amount of seconds to wait till the destroy.
Destroy(sound.gameObject, 3);
}
}
If you are just trying to implement a double jump just set a bool that returns false after the double jump and disallows jumping until the player has landed. Don’t restrict the jump to 3 presses within five seconds. That’s a possible solution, but not a very good one. Can I ask why you would want to restrict the jump mechanism in this way?
Thanks for replying! At first I tried the boolean but just couldnt get it to work. The double jump is working, but with a specific jump timing, for whatever reason it allows you to triple jump. Im not 100% sure how to deal with the bug
I’m not great with JavaScript I prefer c#, but this will most likely work for what you are trying to do. You were setting the double jump int back to zero after it hit three. You don’t want to do that cause it will let you jump forever even if you haven’t hit the ground yet.
Try this:
#pragma strict
var rotationSpeed = 100;
var jumpHeight = 8;
var jumpHeight2 = 16;
var doubleJump : boolean = false;
var doubleJumpCount : int = 0;
var ballSound : Transform;
var distToGround : float;
function Start ()
{
// Getting the distance from the center of our ball to the ground.
distToGround = GetComponent.<Collider>().bounds.extents.y;
}
function IsGrounded() : boolean
{
// Making function, we call this to check if we are on the ground.
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.075);
}
function Update ()
{
if (IsGrounded () )
{
doubleJumpCount = 0;
}
// Making the ball rotation, and controls.
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation);
//Making us jump, then checking if object is on the ground.
if (Input.GetKeyDown (KeyCode.Space))//. && IsGrounded ())
{
if (doubleJumpCount < 2)
{
GetComponent.<Rigidbody>().velocity.y = jumpHeight;
doubleJumpCount += 1;
}
var sound = Instantiate (ballSound, transform.position, transform.rotation);
//Here we destroy the variable to clean our hierarchy. "3" is the amount of seconds to wait till the destroy.
Destroy(sound.gameObject, 3);
}
}
I would tend to agree. However, he doesn’t need a timed event. He just needs a better check to see if he is grounded after the double jump I think. If not grounded then don’t allow any more jumps until the player is. It should be pretty straight forward stuff.