I’m making a Rhythm game and trying to figure out how to write a function where if you hit the action button but the hitbox doesn’t collide with anything then the player loses a point of health. Here’s the current code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hitbox_Trigger : MonoBehaviour {
public IEnumerator DoAttack()
{
GetComponent<BoxCollider>().enabled = true;
GetComponent<MeshRenderer>().enabled = true;
yield return new WaitForSeconds(.1f);
GetComponent<BoxCollider>().enabled = false;
GetComponent<MeshRenderer>().enabled = false;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("z"))
{
StartCoroutine(DoAttack());
}
}
}
Any help is much appreciated. Thank you!