Question

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;
    }
}
}
Was it helpful?

Solution 2

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.

OTHER TIPS

If I understand you correctly... you want to use this script "DynamicAI.cs" on multiple gameObjects and have an if-statement in the code apply to only one of the gameObjects that is using the script.

You can identify the object in the if statement in several ways:

  • You could tag or use a layer on the gameObject and then check for the tag in the if statement
  • You could put a boolean in the script, add it to the if-statement and only assign it true in the one gameObject you want to have the if-statement apply

The easiest way would be to leave that code out of the "DynamicAI.cs" script and put it in another script. Then just attach that script to the one gameObject.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top