示例#1
0
 // Use this for initialization
 void Start()
 {
     baseScript = gameObject.GetComponent <TacticalAI.BaseScript>();
 }
    void AttachAIComponents(GameObject modelBase, Transform spineBoneTransform, Transform targetParentTrans, AnimatorController animController, Transform bulletSpawnTransform, Avatar ava, Transform eye)
    {
        //Create parent object and move it to the position of the model base
        GameObject parentObj = new GameObject();

        parentObj.name = agentName;
        parentObj.transform.position = modelBase.transform.position;

        //parent model base to parent object
        modelBase.transform.parent = parentObj.transform;

        parentObj.AddComponent(typeof(UnityEngine.AI.NavMeshAgent));
        parentObj.AddComponent(typeof(AudioSource));
        if (bulletSpawnerTransform)
        {
            bulletSpawnerTransform.gameObject.AddComponent(typeof(AudioSource));
        }

        parentObj.GetComponent <UnityEngine.AI.NavMeshAgent>().acceleration     = 30;
        parentObj.GetComponent <UnityEngine.AI.NavMeshAgent>().angularSpeed     = 100000;
        parentObj.GetComponent <UnityEngine.AI.NavMeshAgent>().stoppingDistance = 2;

        TacticalAI.BaseScript           newBaseScript           = (TacticalAI.BaseScript)parentObj.AddComponent(typeof(TacticalAI.BaseScript));
        TacticalAI.RotateToAimGunScript newRotateToAimGunScript = (TacticalAI.RotateToAimGunScript)parentObj.AddComponent(typeof(TacticalAI.RotateToAimGunScript));
        TacticalAI.HealthScript         newHealthScript         = (TacticalAI.HealthScript)parentObj.AddComponent(typeof(TacticalAI.HealthScript));
        TacticalAI.SoundScript          newSoundScript          = (TacticalAI.SoundScript)parentObj.AddComponent(typeof(TacticalAI.SoundScript));
        TacticalAI.CoverFinderScript    newCoverFinderScript    = (TacticalAI.CoverFinderScript)parentObj.AddComponent(typeof(TacticalAI.CoverFinderScript));
        TacticalAI.GunScript            newGunScript            = (TacticalAI.GunScript)parentObj.AddComponent(typeof(TacticalAI.GunScript));
        TacticalAI.AnimationScript      newAnimationScript      = (TacticalAI.AnimationScript)parentObj.AddComponent(typeof(TacticalAI.AnimationScript));


        //Attach Animator to to modelBase if one doesn't exist
        if (!modelBase.GetComponent <Animator>())
        {
            modelBase.AddComponent(typeof(Animator));
        }

        //Assign controller to animator
        modelBase.GetComponent <Animator>().runtimeAnimatorController = animController;
        modelBase.GetComponent <Animator>().avatar = ava;

        //Recursively Attach hitboxes to all colliders
        AttachHitboxes(modelBase.transform, newHealthScript);

        //Create target object
        //TacticalAI.TargetScript currTarScript = (TacticalAI.TargetScript) targetObjParentTransform.gameObject.AddComponent(typeof(TacticalAI.TargetScript));
        TacticalAI.TargetScript currTarScript = (TacticalAI.TargetScript)parentObj.AddComponent(typeof(TacticalAI.TargetScript));
        currTarScript.targetObjectTransform = targetObjParentTransform;
        currTarScript.myTeamID          = teamNumber;
        currTarScript.alliedTeamsIDs    = new int[1];
        currTarScript.alliedTeamsIDs[0] = teamNumber;
        currTarScript.enemyTeamsIDs     = new int[1];
        currTarScript.enemyTeamsIDs[0]  = enemyTeamNumber;
        currTarScript.myAIBaseScript    = newBaseScript;
        currTarScript.eyeTransform      = eye;

        //Set connections
        newBaseScript.gunScript         = newGunScript;
        newBaseScript.audioScript       = newSoundScript;
        newBaseScript.headLookScript    = newRotateToAimGunScript;
        newBaseScript.animationScript   = newAnimationScript;
        newBaseScript.coverFinderScript = newCoverFinderScript;

        newRotateToAimGunScript.spineBone            = spineBoneToRotate;
        newRotateToAimGunScript.bulletSpawnTransform = bulletSpawnTransform;
        //newRotateToAimGunScript.myBodyTransform = modelBase.transform;

        newHealthScript.rigidbodies = modelBase.GetComponentsInChildren <Rigidbody>();

        for (var r = 0; r < newHealthScript.rigidbodies.Length; r++)
        {
            newHealthScript.rigidbodies[r].isKinematic = true;
        }


        newHealthScript.collidersToEnable    = modelBase.GetComponentsInChildren <Collider>();
        newHealthScript.gunScript            = newGunScript;
        newHealthScript.rotateToAimGunScript = newRotateToAimGunScript;
        newHealthScript.animator             = modelBase.GetComponent <Animator>();
        newHealthScript.myTargetScript       = currTarScript;
        newHealthScript.myAIBaseScript       = newBaseScript;
        newHealthScript.soundScript          = newSoundScript;

        newGunScript.myAIBaseScript  = newBaseScript;
        newGunScript.animationScript = newAnimationScript;
        newGunScript.bulletSpawn     = bulletSpawnerTransform;
        newGunScript.bulletObject    = bulletObject;
        newGunScript.bulletSound     = bulletSound;

        newAnimationScript.myBaseScript      = newBaseScript;
        newAnimationScript.rotateGunScript   = newRotateToAimGunScript;
        newAnimationScript.myAIBodyTransform = modelBase.transform;
        newAnimationScript.animator          = modelBase.GetComponent <Animator>();
        newAnimationScript.gunScript         = newGunScript;
        newAnimationScript.turnSpeed         = 7.0f;
    }