I wrote a simple coroutine that increases the character speed for half a second (FYI Creating the illusion of a dogde). This is the code
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float characterSpeed = 0.10F;
void FixedUpdate()
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector2.up * characterSpeed);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector2.left * characterSpeed);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector2.down * characterSpeed);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector2.right * characterSpeed);
}
if (Input.GetKey(KeyCode.LeftShift))
{
characterSpeed = 0.2F;
} else {
characterSpeed = 0.10F;
}
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine("Roll");
}
}
IEnumerator Roll()
{
characterSpeed = 0.20f;
yield return new WaitForSeconds(0.5f);
characterSpeed = 0.10f;
}
}
The Coroutine is called “Roll” and as far as i know everything is correct. I used the Unity Manual.
Now everytime i press space character Speed increases. But only for 1 or so frame (Used the Console to read the value). Why is that so?