hey, I am making a little platformer using Unity2D. I have a smoke animation that plays under my characters feet when I jump (pressing the spacebar). And it Sorta works, the smoke animation plays once when I press the space bar.
Right now, I just have a script that Disables the Sprite Renderer until I press “Space”. (after pressing “Space” The Sprite Renderer is enabled for a second) Then after a second (using timer) it Re-Disables the Sprite Renderer. But this only happens once. How can I fix it, so the animation plays EVERY TIME I press the spacebar?
Here is the Script for the Smoke Animation:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Sprites;
public class JumpDust : MonoBehaviour
{
public SpriteRenderer spriteRenderer;
public int lifeTime = 1;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
StartCoroutine(WaitThenDie());
if (Input.GetKeyDown(KeyCode.Space))
{
spriteRenderer.enabled = true;
}
IEnumerator WaitThenDie()
{
yield return new WaitForSeconds(lifeTime);
spriteRenderer.enabled = false;
}
}
}