Greetings,
I’m using the character controller in Unity, to get my player to jump. How would I get the character to delay their jump by 0.4 seconds? I want the delayed jump to last 5 seconds. Any help is appreciated.
Thanks in advance.
Greetings,
I’m using the character controller in Unity, to get my player to jump. How would I get the character to delay their jump by 0.4 seconds? I want the delayed jump to last 5 seconds. Any help is appreciated.
Thanks in advance.
by delay do you mean…
player hits jump, N seconds pass, then jump
or do you mean…
player jumps, player must wait N seconds to pass before they can jump again
In the former you need to have some type of wait for N seconds ability. I use a ‘timer’ object for it that I wrote that will activate a delegate after N seconds pass (set as a property).
The latter requires just having a cool down timer. Which again is just a timer/value that counts down for N seconds. And as long as it’s running you don’t allow the jump.
This all surrounds the Time.deltaTime property:
this is helpful for knowing how much time passed since last update.
or you could use Invoke method with a delay
I need to delay jump when player collides with an object/wall for 0.4 seconds.
I want the delayed jump to last 5 seconds
This is what I have thus far:
using UnityEngine;
using System.Collections;
public class Jumpscript : MonoBehaviour
{
private bool canJump = true;
void OnCollisionEnter(Collision collision)
{
print ("I'm Here");
canJump = false;
Invoke("changeCanJump", 3);
}
public float speed = 10.0f;
public float jumpForce = 10.0f;
public float airModifier = 0.2f;
public void Update()
{
bool grounded;
//is the user pressing left or right (or "a "d") on the keyboard?
Vector3 horMovement = Input.GetAxis("Horizontal") * transform.right * Time.deltaTime * speed;
//is the user pressing up or down (or "w" "s") on the keyboard?
Vector3 forwardMovement = Input.GetAxis("Vertical") * transform.forward * Time.deltaTime * speed;
//are we grounded?
if (Physics.Raycast(transform.position, -transform.up, 2))
{
grounded = true;
}
else
{
horMovement *= airModifier;
forwardMovement *= airModifier;
grounded = false;
}
//jump if the user pressing the space key AND our character is grounded
if (Input.GetKeyUp("space") grounded canJump)
{
rigidbody.AddRelativeForce(transform.up * jumpForce, ForceMode.Impulse);
}
//move our character
transform.Translate(forwardMovement + horMovement);
}
void changeCanJump()
{
canJump = true;
}
}
And it doesn’t work?
try:
if (Input.GetKeyUp("space") grounded)
{
// rigidbody.AddRelativeForce(transform.up * jumpForce, ForceMode.Impulse);
jumpFUNC ();
}
Function JumpFUNC ()
{
If (canJump == true)
{
YeildWaitForSeconds (0.5);
If (canJump == true)
{
rigidbody.AddRelativeForce(transform.up * jumpForce, ForceMode.Impulse);
}
}
}
That is not tested, and will have syntax problems and spelling mistakes, but that for the basis? Presuming canJump is when you are touching the wall? Oh and mine is JS, but its nearly identical.