Hello, I am having a problem with function StartCoroutine().
I’m making small Third Person Game and wanted to synchronize jump function with jump animation. As for now my jump animation is making my character squat and hop, so proper jump is around ~1,5 sec into animation.
When I trigger both animation and jump function at a same time, character is in the air way to soon (at “squating moment”), so I wanted to delay jump function by that ~1,5 seconds using Coroutine but this way game doesn’t change position of a character. I make a good use of debug.log to see if variables are changing and everything looks fine, but in an editor there is absolutely no reaction – position.y of player model is standing still as water.
After two days of trying I can’t get it to work. My code looks like this:
void Start()
{
anim = GetComponent<Animator>();
_controller = GetComponent<CharacterController>();
_groundChecker = transform.GetChild(0);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && _isGrounded && !isJumping)
{
isJumping = true;
anim.SetTrigger("Jumping");
StartCoroutine(JumpDelay(2));
//_velocity.y += Mathf.Sqrt(JumpHeight * -5f * Gravity);
Invoke("ResetJump", jumpDelay);
}
_velocity.y += Gravity * Time.deltaTime;
_controller.Move(_velocity * Time.deltaTime);
}
public void ResetJump()
{
isJumping = false;
}
IEnumerator JumpDelay(float delay)
{
yield return new WaitForSeconds(delay);
Debug.Log(_velocity.y); //velocity before jumping
Jump();
Debug.Log(_velocity.y); //velocity after jumping
}
public void Jump()
{
Debug.Log("Jump!");
_velocity.y += Mathf.Sqrt(JumpHeight * -15f * Gravity);
}
On line 24. there is my jump function and if I call it from there character jumps fine. When I place same line of code into Coroutine (Jump Delay function) variables inside velocity.y are changing but ingame character is standing still. At this point I have no idea what could be wrong, as without coroutine everything is fine (despite of being out of sync). Maybe there is other, better way to synchronize jumping script with animation?