Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is the script for the zombie. For this section, I've cut out most of the script to only include the audio portions.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_Zombie : Entity_Parent {
//We create reference to the AudioSource that we can drag in because the zombies are a prefab
public AudioSource Source;
//This is the Audioclip of the zombie dying. Audio clips are made by downloading sounds from the internet and dragging them into the asset window
public AudioClip DieClip;
//This is the clip for a zombie snarling. It plays periodically.
public AudioClip SnarlClip;
//This is the time between the zombies snarling
public float AudioInterval;
void Start () {
StartCoroutine(PlayGrowl());
}
// Update is called once per frame
void Update () {
CheckAttack();
if(current_Health_Level <= 0)
{
//PlayOneShot is a command from the AudioSource Component which lets us play any audio clip we feed into the function
Source.PlayOneShot(DieClip);
Die();
}
}
//Here we define a coroutine that will use PlayOneShot to play the snarlclips every AudioInterval seconds with an addtional random range
IEnumerator PlayGrowl(){
for(;;){
Source.PlayOneShot(SnarlClip);
yield return new WaitForSeconds(AudioInterval + Random.Range(0, 10.0f));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment