- Home /
Only able to Hit the Z key once every 10 seconds
My Z keys gets him to high jump. That works. But as soon as he lands, he can high jump again. I am trying to make it so you need to wait 10 seconds before doing it again, but as you can see, I failed pretty badly lol.
private bool canHighJump;
private bool isGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
void Start()
{
rb = GetComponent<Rigidbody2D>();
//Set a starting velocity when our Game Starts (Set x to 1 for moving Right and -1 for moving Left)
Vector3 DefaultVelocity = new Vector3(1 * spead, 0, 0);
rb.velocity = DefaultVelocity;
moveDir = spead;
}
//---------------------------------------------------------------------------------------------------------
void FixedUpdate()
{
//jump
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
//move left and right with left and right arrow keys
moveInput = moveInput = Input.GetAxisRaw("Horizontal"); //moveInput = Input.GetAxis("Horizontal") caused him to brifely stop when hitting the arrow again. So changed it to "GetAxisRaw" fixed it
//True if there is no Input
if (moveInput == 0f)
{
//We will try to use rb.velocity.x existing Velocity/Movement instead
//True if our Object is already moving
if (rb.velocity.x > 0f) //True if Moving Right
moveInput = 1f;
else if (rb.velocity.x < 0f) //True if Moving Left
moveInput = -1f;
//If both Statements failed, there is no Horizontal Movement
//moveInput will continue to be at 0
}
//True if there is Input OR we managed to use the existing Velocity instead to fill the value of moveInput
if (moveInput != 0f)
//Continue as per normal
rb.velocity = new Vector3(moveInput * spead, rb.velocity.y);
if (facingRight == false && moveInput > 0)
{
Flip();
}
else if (facingRight == true && moveInput < 0)
{ //because he is moving left while facing right
Flip();
}
}
//------------------------------------------------------------------------
public void Update()
//press key to jump
{
float input = Input.GetAxis("Horizontal");
if (input > 0 && moveDir < 0)
{
moveDir = spead;
Flip();
}
else if (input < 0 && moveDir > 0)
{
moveDir = -spead;
Flip();
}
//CODING THE SPACE BAR TO JUMP
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce);
rb.velocity = Vector3.up * jumpForce;
SoundManager.PlaySound("jump");
}
rb.velocity = new Vector2(moveDir, rb.velocity.y);
//CODING THE Z KEY TO HIGH JUMP
if (isGrounded && Input.GetKeyDown(KeyCode.Z))
{
rb.AddForce(Vector3.up * highJumpForce);
rb.velocity = Vector3.up * highJumpForce;
canHighJump = true;
Physics2D.IgnoreLayerCollision(10, 8, true); //disables the two layer collisions after hitting Z
StartCoroutine(EnablePlayerCollider2D());
StartCoroutine("HighJumpCoolDown");
}
}
private IEnumerator EnablePlayerCollider2D() //re-enable the two layer collisions
{
yield return new WaitForSeconds(0.5f); // wait 1/10th of a second 0.1f
Physics2D.IgnoreLayerCollision(10, 8, false); //re-enables the two layer collisions after landing back down
}
private IEnumerator HighJumpCoolDown() //cant high jump till cool down is up
{
yield return new WaitForSeconds(10f); // cooldown is 10 seconds
CanHighJump();
}
private void CanHighJump()
{
canHighJump = false;
}
Answer by Aviryx · Aug 20, 2020 at 12:39 AM
Try using this standalone script and see if you get the debug messages.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HighJumpTest : MonoBehaviour
{
public bool canHighJump;
void Start()
{
canHighJump = true;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Z))
{
if (canHighJump)
{
Debug.Log("Can High Jump");
StartCoroutine(HighJumpCooldown());
}
}
}
private IEnumerator HighJumpCooldown()
{
canHighJump = false;
Debug.Log("Can Not High Jump");
yield return new WaitForSeconds(10f);
canHighJump = true;
Debug.Log("Can High Jump");
}
}
this caused him to not being able to high jump at all after pressing Z. Removing the " canHighJump = true;" in the Z key section didnt work either.
//CODING THE Z KEY TO HIGH JU$$anonymous$$P
if (isGrounded && Input.GetKeyDown(KeyCode.Z))
{
if (canHighJump)
{
rb.AddForce(Vector3.up * highJumpForce);
rb.velocity = Vector3.up * highJumpForce;
canHighJump = true;
Physics2D.IgnoreLayerCollision(10, 8, true); //disables the two layer collisions after hitting Z
StartCoroutine(EnablePlayerCollider2D());
StartCoroutine(HighJumpCoolDown());
}
}
}
private IEnumerator EnablePlayerCollider2D() //re-enable the two layer collisions
{
yield return new WaitForSeconds(0.5f); // wait 1/10th of a second 0.1f
Physics2D.IgnoreLayerCollision(10, 8, false); //re-enables the two layer collisions after landing back down
}
private IEnumerator HighJumpCoolDown() //cant high jump till cool down is up
{
canHighJump = false;
yield return new WaitForSeconds(10f); // cooldown is 10 seconds
canHighJump = true;
}
Ah. Just needed one simple line of code in the start method lol. it works now thanks! I will accept your answer as the correct one.
Your answer
Follow this Question
Related Questions
gameobject not setting active after timer ends 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Getting a chest is not enabling my collider 0 Answers
Help with making a map based game! 3 Answers