只有一个游戏对象受脚本影响

时间:2014-02-28 00:40:05

标签: c# unity3d fsm

制作状态机AI“游戏”我有3种状态运动,战斗和视野。他们都工作得非常好,现在我正在逃跑(顺便说一句,如果有人有任何良好的教程或信息链接,我会喜欢它的状态)状态,所以我想在他们的空闲中做出所有三个以前的状态模式。当我有1个人工智能时,它可以完美运行一旦我把剩下的(现在总共6个)它仍然只会影响1,但是一段时间之后它会“释放”我想它是脚本的最佳术语并且只是回到所谓的三个状态闲着我认为在我的硬币脚本中我说找到并且我认为它需要成为别的东西,我只是不确定这是否正确但是基于我以前的经验,这通常是Unity的情况。我会发布一些代码,但是这里有一些信息可以帮助你的眼睛不必阅读所有内容:

- 我在我的AIClass中设置了一个bool,在我的一个游戏对象与一个'硬币'发生碰撞之前是假的,而硬币是我在游戏中放入测试我的AI行为的游戏对象。当那个bool是真的时候,在我的AIClass的更新中(对不起,它位于脚本的最底部,你可以向下滚动,但我不得不放置其他代码以防万一我正在做一些事情来引起我的问​​题。)它将MovementState的状态设置为Idle,将CombatState设置为Idle,将ViewState设置为Idle。

- 此外,当bool为false时,是唯一一次发生状态转换的调用。例如,通常情况下,AI会搜索到找到玩家,如果他们发现他们靠近他,如果他们在一定距离内他们就会开火。很简单。当调用'NextMovementState()'或'NextCombatState()'或'NextViewState()'时,这些状态之间的移动发生在每个IENumerator的末尾。因此,当bool为true时,永远不应该调用它们,停止AI移动到除了空闲之外的另一个状态(在更新中bool为真时的设置)。

当我在游戏中只有1个AI时,这很好用。

CoinScript.cs

using UnityEngine;
using System.Collections;

public class CoinScript : MonoBehaviour {

    private AIClass a;
    // Use this for initialization
    void Start () {
        a = GameObject.Find ("Enemy").GetComponent<AIClass>();
    }

    // Update is called once per frame
    void Update () {

    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.collider)
        {
            if(collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "EnemyProjectile")
            {
                Physics.IgnoreCollision(rigidbody.collider,collision.collider);
                //Debug.Log ("Enemy");
            }
            if(collision.gameObject.tag == "Player")
            {
                Debug.Log ("triggered!");
                a.fleeBool = true;
                Destroy(gameObject);
            }
        }
    }
}

AIClass.cs

using UnityEngine;
using System.Collections;

public class AIClass : MonoBehaviour
{
    public NavMeshAgent agent;

    //Ammo in gun before reloading is required
    public int ammo = 30;
    public int maxAmmo = 30;
    //Number of bullets a gun fires in sequence on 1 trigger pull
    public int chamber = 3;
    public int maxChamber = 3;
    //Pause between bursts or mouse presses, set to 0 for fully automatic
    public double chamberTime = 120;
    //How fast a gun fires in RPS
    public int fireRate = 7;
    public int fireTimer = 0;
    //How fast a gun can reload
    public int reloadTime = 3;
    public int reloadTimer = 0;
    //Number of bullets fired per shot
    public int bulletsFired = 1;
    public GameObject bulletClone;

    //Acceptable degrees as to which the AI will begin firing at its target
    public int firingAngle = 5;
    //Vision cone of degrees to left and right
    public int visionAngle = 35;
    public int visionDistance = 100;
    public int vRotationSpeed = 3;
    public int vIdleTimer = 0;
    public int vIdleTime = 300;
    public int searchTimer = 0;
    public int searchTime = 300;

    public int mIdleTimer = 0;
    public int mIdleTime = 300;


    public bool isFocusedOnPlayer = false;
    public bool seesPlayer = false;
    //
    public bool fleeBool;
    public bool flee;
    public enum MovementState
    {
        MSearch,
        MMoving,
        MIdle,
    }
    public enum CombatState
    {
        CFiring,
        CReloading,
        CIdle,
    }
    public enum ViewState
    {
        VSearch,
        VFocus,
        VIdle,
    }

    public enum FleeState
    {
        FSearch,
        FMoving,
        FIdle
    }
    public CombatState combatState;
    public ViewState viewState;
    public MovementState movementState;
    public FleeState fleeState;

    //Search state (knows where player is and will head to the player's location)
    IEnumerator MSearchState ()
    {   
        mIdleTimer = 0;
        int stuckTimer = 0;

        while (movementState == MovementState.MSearch)
        {
            //I've arrived at my location, if idle too long, then go back to idle state
            if(Vector3.Distance(transform.position,agent.destination) < 3)
                mIdleTimer++;
            //I'm stuck and haven't moved in a while, go back to idle state
            if(agent.velocity.magnitude < 1)
                stuckTimer++;
            if(seesPlayer || mIdleTimer > mIdleTime + 200 || stuckTimer > 300)
            {
                agent.destination = transform.position;
                movementState = MovementState.MIdle;
            }
            yield return 0;
        }
        if (!flee) {
                        NextMovementState();
                }

    }
    //Wander state
    IEnumerator MMovingState ()
    {   
        while (movementState == MovementState.MMoving)
        {
            //Wander code... Create a random angle and convert it to radians
            float randomAngle = (float)(3.14/180)*Random.Range(0,360);
            //Normalize direction vector, as we will be using it to calculate where we place the circle
            Vector3 tempV = agent.velocity;
            Vector3.Normalize (tempV);
            //Using our relative position, 5 units in front of us. Use the generated angle to find the point on the circle that we want to go to
            agent.destination =  transform.position + tempV * 3 + new Vector3(Mathf.Cos (randomAngle)*3,0,Mathf.Sin (randomAngle)*3);
            //Check to see if we are within the arena bounds, if not, push our projected vector back inside
            if(agent.destination.x > 24)
                agent.destination = agent.destination + new Vector3(-7,0,0);
            if(agent.destination.x < -24)
                agent.destination = agent.destination + new Vector3(7,0,0);
            if(agent.destination.z > 24)
                agent.destination = agent.destination + new Vector3(0,0,-7);
            if(agent.destination.z < -24)
                agent.destination = agent.destination + new Vector3(0,0,7);

            if(seesPlayer)
            {
                agent.destination = transform.position;
                movementState = MovementState.MIdle;
            }
            yield return 0;
        }
        if (!flee) {
                        NextMovementState ();
                }
    }
    //Not moving, if I don't see the player for awhile, then go wander
    IEnumerator MIdleState ()
    {   
        mIdleTimer = 0;
        while (movementState == MovementState.MIdle)
        {
            if(seesPlayer)
                mIdleTimer = 0;
            else
                mIdleTimer++;
            if(mIdleTimer > mIdleTime + Random.Range (-100,100))
                movementState = MovementState.MMoving;
            yield return 0;
        }
        if (!flee) {
                        NextMovementState ();
                }
    }
    //Visual search state, randomly look around and check to see if we see the player
    IEnumerator VSearchState ()
    {   
        Transform target = GameObject.FindWithTag ("Player").transform;
        Vector3 targetPosition = target.position;
        while (viewState == ViewState.VSearch)
        {
            searchTimer--;
            //Vision Cone calculation
            Vector3 targetDir = target.position - transform.position;
            Vector3 forward = transform.forward;
            float angle = Vector3.Angle(targetDir, forward);
            //If player is within vision cone then proceed
            if (angle < visionAngle)
            {
                //Check to see if there are any object between player and myself
                RaycastHit hit;
                if (Physics.Raycast(transform.position, targetDir, out hit))
                {
                    if(hit.transform == target)
                    {
                        seesPlayer = true;
                        viewState = ViewState.VFocus;
                    }
                }
            }
            //Look in another direction
            if(searchTimer < 0)
            {
                searchTimer = searchTime + Random.Range (-100,100);
                targetPosition = new Vector3(Random.Range (-100,100),transform.position.y,Random.Range (-100,100));
            }
            transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(targetPosition - transform.position), vRotationSpeed*Time.deltaTime);

            yield return 0;
        }
        if (!flee) {
                        NextViewState ();
                }
    }
    //Focus on player
    IEnumerator VFocusState ()
    {   
        Transform target = GameObject.FindWithTag ("Player").transform;
        while (viewState == ViewState.VFocus)
        {
            //Vision Cone calculation
            Vector3 targetDir = target.position - transform.position;
            Vector3 forward = transform.forward;
            float angle = Vector3.Angle(targetDir, forward);
            if (angle > visionAngle)
                viewState = ViewState.VIdle;
            else
            {
                RaycastHit hit;
                //Check if there are any objects in the way
                if (Physics.Raycast(transform.position, targetDir, out hit))
                {
                    if(hit.transform == target)
                    {
                        //Tell other AI where player is
                        GameObject[] objArray = GameObject.FindGameObjectsWithTag ("Enemy");
                        AIClass[] enemyArray = new AIClass[objArray.Length];
                        for(int i = 0; i < enemyArray.Length; i++)
                        {
                            enemyArray[i] = (AIClass)objArray[i].GetComponent(typeof(AIClass));
                            if(i >= enemyArray.Length/2)
                                enemyArray[i].agent.destination = target.position;
                            else
                                enemyArray[i].agent.destination = target.position + target.forward*5;
                            enemyArray[i].movementState = MovementState.MSearch;
                        }
                        seesPlayer = true;
                        transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(target.position - transform.position), vRotationSpeed*Time.deltaTime);
                        //Check to see player is within sights of the gun
                        if (angle < firingAngle)
                            isFocusedOnPlayer = true;
                        else
                            isFocusedOnPlayer = false;
                    }
                    else
                    {
                        //I no longer see the player
                        seesPlayer = false;
                        viewState = ViewState.VIdle;
                        isFocusedOnPlayer = false;
                    }
                }
            }
            yield return 0;
        }
        if (!flee) {
                        NextViewState ();
                }
    }
    //Visual idle state, basically the ai is just looking forward
    IEnumerator VIdleState ()
    {   
        vIdleTimer = 0;
        Transform target = GameObject.FindWithTag ("Player").transform;
        while (viewState == ViewState.VIdle)
        {
            //Vision cone calculation
            vIdleTimer++;
            Vector3 targetDir = target.position - transform.position;
            Vector3 forward = transform.forward;
            float angle = Vector3.Angle(targetDir, forward);
            //Check to see if there is an object is between the ai and the player
            if (angle < visionAngle)
            {
                RaycastHit hit;
                if (Physics.Raycast(transform.position, targetDir, out hit))
                {
                    if(hit.transform == target)
                    {
                        seesPlayer = true;
                        viewState = ViewState.VFocus;
                    }
                }
            }
            if(vIdleTimer > vIdleTime)
                viewState = ViewState.VSearch;
            yield return 0;
        }
        if (!flee) {
                        NextViewState ();
                }
    }
    //Firing gun state
    IEnumerator CFiringState ()
    {   
        while (combatState == CombatState.CFiring)
        {
            if(!isFocusedOnPlayer)
                combatState = CombatState.CIdle;
            fireTimer--;
            if(ammo > 0)
            {
                if(chamber > 0)
                {
                    if(fireTimer <= 0)
                    {
                        for(int i = 0; i < bulletsFired;i++)
                        {
                            GameObject temp = (GameObject) Instantiate (bulletClone,transform.position + transform.forward,transform.rotation);
                            temp.rigidbody.AddForce(transform.forward*500);
                        }
                        fireTimer = 60 / fireRate;
                        ammo--;
                        chamber--;
                    }
                }
                else
                {
                    chamber = maxChamber;
                    fireTimer = (int)(60/chamberTime);
                }
            }
            else
            {
                combatState = CombatState.CReloading;
            }
            yield return 0;
        }
        if (!flee) {
                        NextCombatState ();
                }
    }

    IEnumerator CReloadingState ()
    {
        reloadTimer = reloadTime * 60;
        while (combatState == CombatState.CReloading)
        {
            reloadTimer--;
            if(reloadTimer <= 0)
            {
                ammo = maxAmmo;
                combatState = CombatState.CIdle;
            }
            yield return 0;
        }
        if (!flee) {
                        NextCombatState ();
                }
    }

    IEnumerator CIdleState ()
    {   
        while (combatState == CombatState.CIdle)
        {
            if(isFocusedOnPlayer)
                combatState = CombatState.CFiring;
            yield return 0;
        }
        if (!flee) {
                        NextCombatState ();
                }
    }

    void Start ()
    {
        fleeBool = false;
        flee = false;
        NextCombatState();
        NextViewState();
        NextMovementState();
    }
    void NextMovementState()
    {   
        string methodName = movementState.ToString() + "State";
        System.Reflection.MethodInfo info = GetType().GetMethod(methodName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        StartCoroutine((IEnumerator)info.Invoke(this, null));
    }
    void NextCombatState ()
    {
        string methodName = combatState.ToString() + "State";
        System.Reflection.MethodInfo info = GetType().GetMethod(methodName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        StartCoroutine((IEnumerator)info.Invoke(this, null));
    }
    void NextViewState ()
    {
        string methodName = viewState.ToString() + "State";
        System.Reflection.MethodInfo info = GetType().GetMethod(methodName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        StartCoroutine((IEnumerator)info.Invoke(this, null));
    }

    void NextFleeState()
    {
        string methodName = viewState.ToString() + "State";
        System.Reflection.MethodInfo info = GetType().GetMethod(methodName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
        StartCoroutine((IEnumerator)info.Invoke(this, null));
    }

    void Update()
    {
        Debug.DrawLine(transform.position, transform.position+transform.forward*5, Color.red);
        if (fleeBool == true) 
        {
            flee = true;
        }

        if (flee == true) 
        {
            Debug.Log ("flee is true");
            combatState = CombatState.CIdle;
            movementState = MovementState.MIdle;
            viewState = ViewState.VIdle;
            Debug.Log ("End of idles");
        }
    }

}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

你可以将所有AIClass存储到数组中然后循环它变得具体,所以每个敌人都会受到影响

AIClass[] AIObject = GameObject.Find("Enemy").GetComponents<AIClass>();  
foreach(AIClass a in AIObject ){  
 //do something with a
}

它应该有用。