- Home /
Coroutine doesnt work. Delayed Jump.
I am making my character Move and Jump. But I want to add a coroutine to the Jump, It has two jumps, Jump2() works fine, but the coroutine is not recognized. Why? Here is the code:
using UnityEngine; using System.Collections;
public class TP_Controller : MonoBehaviour {
public static CharacterController CharacterController;
public static TP_Controller Instance;
void Awake()
{
CharacterController = GetComponent("CharacterController") as CharacterController;
Instance = this;
}
void Update()
{
GetLocomotionInput();
HandleActionInput();
TP_Motor.Instance.UpdateMotor();
DelayedJump();
}
void GetLocomotionInput()
{
var deadZone = 0.1f;
TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
TP_Motor.Instance.MoveVector = Vector3.zero;
if (((Input.GetAxis("Vertical") > deadZone) && !Input.GetKey(KeyCode.LeftShift)) ||
(Input.GetAxis("Vertical") < -deadZone && !Input.GetKey(KeyCode.LeftShift)))
TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical")); // Moves the character vertically
if (Input.GetKey(KeyCode.LeftShift) & Input.GetKey("d"))
TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0); // Makes the character strafe Right
if (Input.GetKey(KeyCode.LeftShift) & Input.GetKey("a"))
TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0); // Makes the character strafe Left
if (((Input.GetAxis("Horizontal") > deadZone) && !Input.GetKey(KeyCode.LeftShift)) ||
(Input.GetAxis("Horizontal") < -deadZone) && !Input.GetKey(KeyCode.LeftShift))
transform.Rotate (0, Input.GetAxis("Horizontal"), 0); // Makes the character rotate around its axis.
TP_Animator.Instance.DetermineCurrentMoveDirection();
}
void HandleActionInput() // Makes the character Jump
{
if (Input.GetKey(KeyCode.Space))
{
StartCoroutine(DelayedJump());
}
else if (Input.GetKey("w") && !Input.GetKey(KeyCode.LeftShift) ||
(Input.GetKey("up")) && !Input.GetKey(KeyCode.LeftShift))
{
Jump2();
}
}
IEnumerator DelayedJump ()
{
yield return new WaitForSeconds(0.5f);
TP_Motor.Instance.Jump();
}
void Jump2()
{
TP_Motor.Instance.Jump2();
}
}
I have another TP_Motor script where the jump is called.
Answer by ScottYann · Aug 03, 2013 at 11:58 PM
You have to call your coroutine like this:
StartCoroutine(DelayedJump());
Otherwise exec will skip over it entirely and not even give you an error. Frustrating isn't it :D
Thanks but I have already done that, check the code. Or am I doing something bad ins$$anonymous$$d???
In your code you have this:
void Update() { GetLocomotionInput();
HandleActionInput();
TP_$$anonymous$$otor.Instance.Update$$anonymous$$otor();
DelayedJump();
}
//delayed jump is in there without it being started as a coroutine... thats where my eye first fell
Your answer
Follow this Question
Related Questions
Delay jump to confirm player really wants to jump 1 Answer
Using a touch event for character motor 0 Answers
jumping and landing not very responsive 1 Answer
Access to float seconds ago 2 Answers