Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Zumbi : MonoBehaviour {
- //Personagem
- public string direcao;
- private float velocidade;
- //Audio
- public AudioClip[] grunidos;
- public float grunidoProbabilidade = 50f; // Chance que o personagem tem para emitir um grunido
- public float grunidoDelay = 1f; // Espaço de tempo entre cada grunido depois de finalizado o anterior
- private int grunidoIndex; // Variavel que irá guardar o grunido atual
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update () {
- if (direcao == "direita") {
- velocidade = 1f;
- transform.Translate (Vector2.right * velocidade * Time.deltaTime);
- } else if (direcao == "esquerda") {
- velocidade = -1f;
- transform.Translate (Vector2.right * (- velocidade) * Time.deltaTime);
- }
- }
- void OnCollisionEnter2D(Collision2D colisor) {
- //Para morrer o personagem caso o zumbi acerte ele.
- if (colisor.gameObject.tag == "Player") {
- Destroy(colisor.gameObject);
- }
- }
- public IEnumerator Grunir()
- {
- // Checa a probabilidade de emitir um grunido
- float grunidoChance = Random.Range(0f, grunidoProbabilidade * 2);
- if(grunidoChance > grunidoProbabilidade){
- // Espera por grunidoDelay.
- yield return new WaitForSeconds(grunidoDelay);
- // Verifica se pode emitir um novo grunido
- if(!audio.isPlaying){
- // Escolhe um grunido, porem diferente do anterior
- grunidoIndex = GrunirRandom();
- // Inicia um novo grunido
- audio.clip = grunidos[grunidoIndex];
- audio.Play();
- }
- }
- }
- public int GrunirRandom()
- {
- // Escolhe uma posição aleatória na array de grunidos
- int auxIndex = Random.Range(0, grunidos.Length);
- // Verifica se é o mesmo grunido emitido anteriormente
- if(auxIndex == grunidoIndex){
- return GrunirRandom();
- }else{
- return auxIndex;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement