- Home /
Delay Jump
Hello Unity Community,
I'm trying to create a slight delayed jump at 1.3 seconds after the player has collided with specific objects. For example, when the user collides with certain objects that have a tag called "Collision" and then they jump, it should take 1.3 seconds before the player jumps.
Here's my code:
using UnityEngine;
using System.Collections;
public class MoveForward : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Start () {
AkSoundEngine.PostEvent("Play_FS_Running", gameObject);
}
// Update is called once per frame
void Update () {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 2);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
AkSoundEngine.PostEvent("Play_Jump", gameObject);
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
void OnControllerColliderHit (ControllerColliderHit OtherObj) {
if (OtherObj.collider.tag == "Collision")
Debug.Log("We've collided");
}
}
The line before 'Debug.Log("We've collided");' is where I need to put the proper code I believe. I've heard that I should look into coroutines but, from my understanding, a coroutine yields methods so it wouldn't work with my current code. Maybe I'm wrong. What I need is to be pointed in the right direction. Any input is appreciated as I've been struggling with this problem for almost 2 months. Seriously.
Thank you for your time.
Answer by pad406 · Jan 09, 2013 at 10:35 PM
You're right about the coroutine but I think you have to split it into a new routine. So at your debug.log statement do this
startcoroutine(myDelay());
Then your myDelay function as follows:
//myDelay function
IEnumerator myDelay()
{
yield return WaitForSeconds(1.3f);
//Whatever code you want to do, ie jump
}
Alternative would be use Invoke
Invoke("MyJumpRoutine",1.3F);
I'm getting an error on the IEnumerator line that says:
error CS1525: Unexpected symbol `('
Here's my code:
void OnControllerColliderHit (ControllerColliderHit OtherObj) { if (OtherObj.collider.tag == "Collision"){
StartCoroutine(myDelay());
IEnumerator myDelay(){
yield return WaitForSeconds(1.3f);
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
AkSoundEngine.PostEvent("Play_Jump", gameObject);
}
}
}
}
Also, I'm not sure if the code to jump (lines after if statement) is correct. :/
Is there a simpler way to put in code to jump after the yield statement? I can tell I'm doing something wrong.
First the myDelay is a seperate routine/function. You can't put it inside the OnControllerCollierHit. Next what you are basically doing there is saying 'When I collide, wait for 1.3 seconds and then if the Jump button is pressed, jump. I'm not sure what you are trying to achieve with both the collider and the Jump. Is the player not allowed jump till 1.3s after they collide or after they hit the jump button? Can they only hit the jump button after they collide? The code will be slightly different depending on the 'rule' your're trying to implement
The player is not allowed to jump till 1.3s after they hit the jump button.
Ok, why not then, at the point where the player is allowed jump, do the following:
if (Input.GetButton("Jump")) { Invoke("$$anonymous$$yJumpRoutine", 1.3F); }
Then have a routine called $$anonymous$$yJumpRoutine which has the code you want for the jump.