For some reason the audio will not play even though it is being called upon in the code.
Here is the code:
using UnityEngine;
using System.Collections;
//Update, the script is now attached to a zombie model animation code is added
public class enemy : MonoBehaviour {
private GameObject wayPoint;
//public float Cube;
private Vector3 wayPointPos;
public Transform zombie_dist;
//This will be the zombie's speed. Adjust as necessary.
public float timer;
private float speed = 3.0f;
public Rigidbody rb;
public GameObject zombie;
Animation Death;
public float dist;
//public CapsuleCollider cc;
public Color newColor;
public bool isClose;
public Transform target;
public float hitBox;
public bool isDead;
public bool isIdle;
public float deathTime; // how long the zombie lives after its first initial death;
//public float attack;
public AudioSource attack_audio;
void Start ()
{
attack_audio = GetComponent<AudioSource> ();
//Attack = attack_audio.GetComponent<AudioSource>();
isIdle = false;
deathTime = 5;
isDead = false;
isClose = false;
Death = zombie.GetComponent<Animation> ();
timer = 10;
//cc = GetComponent<CapsuleCollider>();
rb = GetComponent<Rigidbody>();
//At the start of the game, the zombies will find the gameobject called wayPoint.
wayPoint = GameObject.Find("wayPoint");
//move = GetComponent.<Animation>();
}
void Update ()
{
if(target != null)
{
transform.LookAt(target);
}
if (zombie_dist) {
dist = Vector3.Distance(zombie_dist.position, transform.position);
print("Distance to other: " + dist);
}
if (dist <= 1.5 && isClose == false) {
speed = 0;
Death.Play("Attacking");
attack_audio.Play();
}
if (dist >= 8 && isClose == false) {
speed = 0;
Death.Play("Idle");
}
if (dist >= 1.6 && isClose == false && dist <= 7) {
speed = 3;
Death.Play("Walking");
}
if (isClose == true) {
timer -= 1 * Time.deltaTime;
speed = 0;
}
if (timer <= 0) {
Destroy(gameObject);
}
wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
//Here, the zombie's will follow the waypoint.
transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
}
//void OnTriggerEnter (Collider other)
//{
// if (other.gameObject.tag == "bullet") {
// gameObject.GetComponent<Renderer>().material.color = Color.blue;
// rb.isKinematic = false;
// speed = 0;
// cc.isTrigger = false;
// newColor = Color.black;
// }
// }
void OnCollisionEnter (Collision col)//if there is a collision with the prefab projectile the the color of the enemy will be blue (M4A1 gun model compadible)
{
if (col.gameObject.name == “projectile(Clone)”) {
gameObject.GetComponent().material.color = Color.blue;
//rb.isKinematic = false;
speed = 0;
newColor = Color.black;
Death.Play (“Dying”);
isClose = true;
gameObject.GetComponent().enabled = false;
}
else if (col.gameObject.name == "bullet_2(Clone)") {
gameObject.GetComponent<Renderer>().material.color = Color.red;
//rb.isKinematic = false;
speed = 0;
newColor = Color.black;
Death.Play ("Dying");
isClose = true;
gameObject.GetComponent<BoxCollider>().enabled = false;
//if the ak-47 model is enabled the the gun will shoot the prefab bullet_2 which make the enemy color red
}
}
}