Hey guys, can I poke your brain for a sec? This is my player control script that I am working on, and currently it works to where I can target and kill enemies, but I am trying to remove them from being targetable (not destroy the gameobject) once they are dead (I have a bool in the EnemyAI class that I set: IsDead) which I am setting in the Fixed Update of this script. Anyways, I have tried about 15 different ways which are all obviously wrong… but the closest I can get it is to have it skip over an enemy in the Enemies[ ] array, but I can’t make it look access it’s IsDead variable.
What I’m really asking here, is in AddAllEnemies() or TargetEnemy() how do I make write this? Be gentle, I’m new. Been following BurgZerg’s tutorial and have learned a lot but am now branching out.
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerControls : MonoBehaviour {
/*MOVEMENT*/
public float speed = 3f; //Walk speed
public float runSpeed = 5f; //Run speed
public float turnSpeed = 30f; //Turn speed
private Vector3 movement;
/*TARGETTING*/
public List<Transform> targets;
public Transform selectedTarget;
public GameObject target;
public float attackTimer;
public float coolDown;
public bool targetDead = false;
private GameObject[] Enemies;
/*SOUNDS*/
public AudioClip Shotgun; //the sound file for shotgun shoost
new AudioSource audio; //required for setting up audio
private Animator anim;
private Rigidbody playerRigidbody;
private Transform myTransform;
void Awake(){
anim = GetComponent<Animator> ();
playerRigidbody = GetComponent<Rigidbody> ();
myTransform = transform; //caches transform since we're using it a lot
}
void Start(){
targets = new List<Transform> ();
AddAllEnemies ();
selectedTarget = null;
attackTimer = 0;
coolDown = 1.0f;
audio = GetComponent<AudioSource> ();
}
// Update is called once per frame
void Update () {
//If you press K, target nearest enemy
if (Input.GetKeyDown (KeyCode.K)) {
TargetEnemy();
}
//If you press U, try to access a container if close enough.
if (Input.GetKeyDown (KeyCode.U)) {
Messenger.Invoke("LookForContainer");
}
//If you press I, toggle inventory window
if (Input.GetKeyDown (KeyCode.I)) {
Messenger.Invoke("ToggleInventoryWindow");
}
if (attackTimer > 0)
attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if (Input.GetKeyUp (KeyCode.J)) {
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
}
void FixedUpdate(){ //This fires every physics update
//If a target is selected, get status of mob's isDead bool
// if (selectedTarget != null)
// targetDead = selectedTarget.GetComponent<EnemyAI> ().isDead;
//if target is dead, deselect it and kill it
if (targetDead) {
Debug.Log ("Killed A Mob");
selectedTarget.GetComponent<EnemyAI>().isDead = true;
selectedTarget.GetComponent<EnemyAI>().KillMe();
DeselectTarget();
}
float h = Input.GetAxisRaw ("Horizontal"); //Raw means it will only return -1, 0, or 1. (so no speed up to go) - Horizontal maps from < or > or A or D
float v = Input.GetAxisRaw ("Vertical"); //Maps from up dwn w s
Move (h, v);
Turning (h);
Animating (h, v);
}
void Move(float h, float v){
if (v == -1) {
myTransform.position -= transform.forward * speed * Time.deltaTime;
}
if (v == 1) {
myTransform.position += transform.forward * speed * Time.deltaTime;
}
}
void Turning(float h){
if (h == -1) {
myTransform.Rotate(-Vector3.up * turnSpeed * Time.deltaTime);
}
if (h == 1) {
myTransform.Rotate (Vector3.up * turnSpeed * Time.deltaTime);
}
}
void Animating(float h, float v){
// bool walking = h != 0f || v != 0f;
bool walking = v != 0f;
anim.SetBool ("IsWalking", walking);
}
public void AddAllEnemies() {
//New array called 'Enemies', and fill it with things tagged as Enemy
Enemies = GameObject.FindGameObjectsWithTag ("Enemy");
//For each loop, create game object called enemy in array 'Enemies'
//gotta somehow skip by dead enemies
foreach (GameObject enemy in Enemies)
AddTarget (enemy.transform);
}
public void AddTarget(Transform enemy)
{
targets.Add (enemy);
}
private void SortTargetsByDistance(){
targets.Sort (delegate(Transform t1, Transform t2) {
//gets distance and returns a float of how far away
return (Vector3.Distance (t1.position, myTransform.position).CompareTo (Vector3.Distance (t2.position, myTransform.position)));
});
}
private void TargetEnemy(){
if (targets.Count == 0)
AddAllEnemies ();
if (targets.Count > 0) {
if (selectedTarget == null) {
SortTargetsByDistance ();
selectedTarget = targets [0];
} else {
int index = targets.IndexOf (selectedTarget); //gives index of selected target and it's relationship to where it is in our list (what?)
if(index < targets.Count - 1) {
index++;
} else {
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
}
private void SelectTarget()
{
// Transform name = selectedTarget.FindChild("Name"); //Seek out a better solution than FindChild if we use this, FIND's tend to be costly
Transform isSelected = selectedTarget.FindChild ("TargetSelected"); //Find the TargetSelected arrow mesh
// if (name == null) {
// Debug.LogError ("Could not find the Name on " + selectedTarget.name);
// return;
// }
// name.GetComponent<TextMesh> ().text = selectedTarget.GetComponent<Mob> ().Name;
// name.GetComponent<MeshRenderer> ().enabled = true;
selectedTarget.GetComponent<Mob> ().DisplayHealth ();
isSelected.GetComponent<MeshRenderer> ().enabled = true;
Messenger<bool>.Invoke("show mob vitalbars", true);
Messenger.AddListener ("Active Mob Died", KillMob);
}
public void DeselectTarget()
{
// selectedTarget.FindChild ("Name").GetComponent<MeshRenderer> ().enabled = false;
selectedTarget.FindChild ("TargetSelected").GetComponent<MeshRenderer> ().enabled = false;
selectedTarget = null;
Messenger<bool>.Invoke("show mob vitalbars", false);
//toggle targetDead back to false to get it ready for the next mob
targetDead = false;
}
private void Attack() {
float distance = Vector3.Distance (selectedTarget.transform.position, transform.position);
Vector3 dir = (selectedTarget.transform.position - transform.position).normalized;
float direction = Vector3.Dot (dir, transform.forward);
Debug.Log (direction);
if (distance < 10) {
if(direction > 0) {
// EnemyHealth eh = (EnemyHealth)selectedTarget.GetComponent ("EnemyHealth");
// eh.AdjustCurrentHealth (-10);
anim.SetBool ("IsAttacking", true);
audio.PlayOneShot (Shotgun, 0.7F);
//Debug.Log ("BOOM I SHOOT U");
RemoveHPFromEnemy();
//new WaitForSeconds (2);
anim.SetBool ("IsDoneAttacking", true);
}
}
}
private void RemoveHPFromEnemy(){
//Transform isSelected = selectedTarget.FindChild ("TargetSelected"); //Find the TargetSelected arrow mesh
// selectedTarget.GetComponent<Mob> ().DisplayHealth ();
int removeHP = selectedTarget.GetComponent<Mob> ().curHealth -= 10;
int mobMaxHP = selectedTarget.GetComponent <Mob> ().maxHealth;
// selectedTarget.GetComponent<Mob> ().maxHealth;
Messenger<int, int>.Invoke("mob health update", removeHP, mobMaxHP);
//Debug.Log ("Did Some Damage");
// Messenger<bool>.Invoke("show mob vitalbars", true);
}
private void KillMob(){
targetDead = true;
}
}