hiya,
Everything with my sounds is working great excepting when the zombie dies.
The sound is working and playing in every other method.
And other sounds re not playing in the problematic method.
The problem method is CheckIfDead
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieController : MonoBehaviour
{
public Rigidbody2D zombieRB;
public PlayerController playerController;
public bool alive = true;
public int zombieLife = 3;
public float walkingSpeed;
public float stoppingDistance;
private float oldPosition;
private bool facingRight;
public Animator zombieAnim;
public bool attack = false;
public Transform target;
public Collider2D zombieCollider;
public AudioSource zombieMoan1;
public AudioSource zombieMoan2;
public AudioSource zombieAttack;
public AudioSource zombieShot;
public AudioSource zombieDie;
private void Start()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
}
private void Update()
{
oldPosition = transform.position.x;
MoveZombie();
ZombieDirection();
FlipPlayer();
CheckIfDead();
ZombieRandomMoans();
//print("zombie life?" + zombieLife);
}
void FlipPlayer()
{
if (alive)
{
Vector3 characterScale = transform.localScale;
if (!facingRight)
{
characterScale.x = -1f;
}
if (facingRight)
{
characterScale.x = 1f;
}
transform.localScale = characterScale;
}
}
private void MoveZombie()
{
if (Vector2.Distance(transform.position, target.position) > stoppingDistance)
{
attack = false;
transform.position = Vector2.MoveTowards(transform.position, target.position, walkingSpeed * Time.deltaTime);
}
if (Vector2.Distance(transform.position, target.position) <= stoppingDistance && playerController.alive) {
if (alive)
{
attack = true;
}
} else
{
attack = false;
}
if (!playerController.alive)
{
zombieAnim.Play("Idle");
}
}
void ZombieDirection()
{
if (transform.position.x > oldPosition)
{
facingRight = true;
}
if (transform.position.x < oldPosition)
{
facingRight = false;
}
}
private void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "Bullet")
{
zombieLife--;
zombieMoan1.Stop();
zombieMoan2.Stop();
zombieShot.Play();
}
}
void CheckIfDead()
{
if (zombieLife <= 0)
{
alive = false;
zombieAnim.SetBool("Dead", true);
stoppingDistance = 500;
zombieCollider.enabled = false;
zombieRB.gravityScale = 0;
//////////////////////////////////////////////////////////wont play any sounds here/////////
}
}
void ZombieRandomMoans()
{
if (alive)
{
int num1 = Random.Range(1, 1000);
print("Rand num = " + num1);
if (num1 == 500)
{
if (!zombieMoan1.isPlaying && !zombieMoan2.isPlaying)
zombieMoan1.Play();
}
if (num1 == 100)
{
if (!zombieMoan1.isPlaying && !zombieMoan2.isPlaying)
zombieMoan2.Play();
}
}
}
}