Hello All,
This is my first time posting. I have a problem and I am trying to figure out a solution.
I created a prefab with a c# script on it. It handles AI states, Running, Walking, Wounded, Killed. It uses a enum so that I can state the state of the AI in the editor.
I noticed an interesting problem. If I duplicate the prefab and kill the prefab that I originally added to the scene. It updates all the other prefabs to the same state.
Here is the code and how its used.
using UnityEngine;
using System.Collections;
public class InfectedStates : MonoBehaviour{
public enum InfectedState{
None,
Idle,
Curious,
Walking,
Running,
Crawling,
Patrolling,
Swarm,
Fleeing,
Attacking,
Death,
Dead,
FacingUp,
FacingDown,
Wounded
};
public static InfectedState currentState = InfectedState.Idle;
public static InfectedState previousState = InfectedState.None;
public void Update(){
print (currentState);
}
public void SetCurrentState(InfectedState state){
currentState = state;
}
public void SetPreviousState(InfectedState state){
previousState = state;
}
}
I am updated the state the following way during run time.
InfectedStates.InfectedState state = InfectedStates.currentState;
InfectedStates.currentState = InfectedStates.InfectedState.Running;
It is also worth noting that I am looping through each of the prefabs to determine if they are still alive and are close enough to come help the wounded/dead AI.
Here is that Code as well.
private void StartSwarm(Vector3 position){
GameObject[] enemies;
enemies = GameObject.FindGameObjectsWithTag("Infected");
GameObject closest;
int inRange = 0;
foreach(GameObject infected in enemies){
Vector3 diff = infected.transform.position - position;
float currentDistance = diff.sqrMagnitude;
print(currentDistance+": Distance to Infected");
if(currentDistance <= DeathSwarmRange){
InfectedStates.InfectedState state = InfectedStates.currentState;
InfectedStates.InfectedState prevState = InfectedStates.previousState;
if(state != InfectedStates.InfectedState.Dead || state != InfectedStates.InfectedState.Death || state != InfectedStates.InfectedState.Swarm || state != InfectedStates.InfectedState.Crawling){
prevState = state;
switch(state){
case InfectedStates.InfectedState.Idle:
InfectedStates.currentState = InfectedStates.InfectedState.Running;
break;
case InfectedStates.InfectedState.Walking:
InfectedStates.currentState = InfectedStates.InfectedState.Running;
break;
case InfectedStates.InfectedState.Running:
infected.GetComponent<NavMeshAgent>().destination = position;
break;
case InfectedStates.InfectedState.Patrolling:
InfectedStates.currentState = InfectedStates.InfectedState.Running;
break;
case InfectedStates.InfectedState.Fleeing:
InfectedStates.currentState = InfectedStates.InfectedState.Running;
break;
case InfectedStates.InfectedState.Attacking:
InfectedStates.currentState = InfectedStates.InfectedState.Running;
break;
case InfectedStates.InfectedState.FacingUp:
InfectedStates.currentState = InfectedStates.InfectedState.Running;
//InfectedState = "Running";
break;
case InfectedStates.InfectedState.FacingDown:
InfectedStates.currentState = InfectedStates.InfectedState.Running;
//InfectedState = "Running";
break;
}
infected.GetComponent<NavMeshAgent>().destination = position;
SwarmUpdates(infected);
inRange++;
}
}
if(inRange < SwarmSize){
//Spawn More
}
if(inRange == 0){
/*
* Spawn Infected
* Randomize Infected Prefab to spawn
*
*/
}
}
}
Thanks.