Disable gravity (freeze character in mid air) without him flying into space?
I'm trying to make it so a character jumps, if the jump button is hit again in mid air the character freezes for 1 second then is given a great stomp force and whatever he collides with breaks or is killed.
I don't know if you're suppose to put the rigid body to sleep, it says it only sleeps for one frame maybe that's the problem. Or it executes the IEnumerator on the first jump even though I think getbuttondown is correct? I have tried moving the code around and even had a small delay at the start of the IEnumerator so the second jump can´t be triggered early but it's not working, any ideas?
//Player jumping
if (Input.GetButtonDown("Jump") && touchesGround)
{
Debug.Log("Jumping");
Jump.Play();
playerRigidBody.AddForce(0f, 325f, 0f);
StartCoroutine("Stomp");
}
else
{
}
}
IEnumerator Stomp ()
{
yield return new WaitForSeconds(0.5f);
if (Input.GetButtonDown("Jump"))
{
Debug.Log("Working");
playerRigidBody.Sleep();
yield return new WaitForSeconds(1.5f);
Debug.Log("1.5 seconds have gone by");
playerRigidBody.WakeUp();
playerRigidBody.AddForce(0f, -325f, 0f);
}
}
Answer by Haugkall · Mar 10, 2018 at 07:49 PM
Ahh so satisfying figuring it out yourself, no need for a second check for jump, you just needed another else if and a variable.
//Player jumping
if (Input.GetButtonDown("Jump") && touchesGround)
{
Jump.Play();
playerRigidBody.AddForce(0f, 325f, 0f);
secondJump = true;
}
else if (Input.GetButtonDown("Jump") && secondJump == true)
{
StartCoroutine("Stomp");
}
else
{
}
}
IEnumerator Stomp()
{
Debug.Log("Working");
playerRigidBody.isKinematic = true;
yield return new WaitForSeconds(0.75f);
playerRigidBody.isKinematic = false;
playerRigidBody.AddForce(0f, -1500f, 0f);
}