Making an AI game, trying to only affect 1 gameobject in an if statement. I think the way I'm doing it is very wrong, and I was advised to create an array of my instance of my enum. I'm just not sure the best way to go about this. This is the code I have (obviously it is wrong though, although it does work, it changes the state of all gameobjects that inherit from my DynamicAI class).
DynamicAI.cs
using UnityEngine;
using System.Collections;
public class DynamicAIClass : MonoBehaviour {
public PlayerScript GO;
public Transform target;
public Transform myTransform2;
public GameObject bullet;
public GameObject Dyn1;
public GameObject Dyn2;
public GameObject Dyn3;
public GameObject Dyn4;
public int maxDistance;
int maxFireDis;
public int rotationSpeed;
public int moveSpeed;
bool StopDyn3;
bool StopDyn4;
public enum FollowAIStates
{
Stalk = 0,
Idle = 1
}
public FollowAIStates DynamicAI = FollowAIStates.Stalk;
void Awake() {
myTransform2 = transform;
}
// Use this for initialization
void Start () {
GO = GameObject.Find ("Player").GetComponent<PlayerScript>();
Dyn3 = GameObject.FindGameObjectWithTag ("Dyn3");
target = GO.transform;
maxDistance = 2;
maxFireDis = 1;
}
// Update is called once per frame
void Update () {
switch (DynamicAI)
{
case FollowAIStates.Stalk:
//handles rotation of AI
myTransform2.rotation = Quaternion.Slerp (myTransform2.rotation, Quaternion.LookRotation(target.position - myTransform2.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform2.position) > maxDistance)
{
//Move towards target
myTransform2.position += myTransform2.forward * moveSpeed * Time.deltaTime;
}
if(Vector3.Distance(target.position, myTransform2.position) > maxFireDis)
{
//Shoots towards target
Instantiate(bullet, transform.position, transform.rotation);
}
if (Dyn3 && GO.transform.position.z > -0.0125f && GO.transform.position.x > -0.00107f)
{
Debug.Log ("STALK");
StopDyn3 = true;
}
else {
StopDyn3 = false;
}
/*if (Dyn4.activeInHierarchy == true && GO.transform.position.z < -0.0125f && GO.transform.position.x < -0.00107f)
{
Debug.Log ("STALK_DYN4");
StopDyn4 = true;
}
else {
StopDyn4 = false;
}*/
if (Dyn3 && !StopDyn3)
{
DynamicAI = FollowAIStates.Idle;
}
/*if (Dyn4 && !StopDyn4)
{
DynamicAI = FollowAIStates.Idle;
}*/
break;
case FollowAIStates.Idle:
if (Dyn3 && GO.transform.position.z > -0.0125f && GO.transform.position.x < -0.00107f)
{
Debug.Log ("IDLE");
DynamicAI = FollowAIStates.Stalk;
}
/*if (Dyn4 && GO.transform.position.z < -0.0125f && GO.transform.position.x < -0.00107f)
{
Debug.Log ("IDLE_DYN4");
DynamicAI = FollowAIStates.Stalk;
}*/
break;
}
}
}
All your GameObjects are being affected because they all use this script, therefore this code is ran in every GameObject. That is, that Update()
method is going to run once in every GameObject that has this script every update cycle.
The way you are making it run in only one or some GameObjects depends a lot on what you want to do. For example, what makes one given GameObject be the one that is supposed to run the code? You should add some logic to only allow the desired object to run Update()
, like this:
void Update() {
if(!hasToken)
return;
// ...
}
And you'd have another script that would pass the token to the right GameObject.