Esempio n. 1
0
        void Update()
        {
            if (Time.time < delay)
            {
                return;
            }
            // if target is exist.
            if (target)
            {
                // rotation facing to the target.
                Quaternion targetlook = Quaternion.LookRotation(target.transform.position - this.transform.position);
                this.transform.rotation = Quaternion.Lerp(this.transform.rotation, targetlook, Time.deltaTime * 3);
                // find a direction from the target
                Vector3 dir       = (target.transform.position - transform.position).normalized;
                float   direction = Vector3.Dot(dir, transform.forward);
                // check if in front direction
                if (direction > 0.9f)
                {
                    if (weapon)
                    {
                        // shooting!!.
                        weapon.LaunchWeapon(indexWeapon);
                    }
                }
                // AI attack the target for a while (3 sec)
                if (Time.time > timeAIattack + 3)
                {
                    target = null;
                    // AI forget this target and try to looking new target
                }
            }
            else
            {
                for (int t = 0; t < TargetTag.Length; t++)
                {
                    // AI find target only in TargetTag list

                    if (AirStrike.AIPool == null)
                    {
                        Debug.LogError("Need AIManager placed in this scene, please go to AirStrike/Prefabs/Game/AIManager and placing AIManager prefab to the scene");
                        return;
                    }
                    TargetCollector targetCollector = AirStrike.AIPool.FindTargetTag(TargetTag [t]);

                    if (targetCollector != null && targetCollector.Targets.Length > 0)
                    {
                        // find all objects in Tags list.
                        float distance = int.MaxValue;
                        for (int i = 0; i < targetCollector.Targets.Length; i++)
                        {
                            if (targetCollector.Targets [i] != null)
                            {
                                // find the distance from the target.
                                float dis = Vector3.Distance(targetCollector.Targets [i].transform.position, transform.position);
                                // check if in ranged.
                                if (distance > dis)
                                {
                                    // Select closer target
                                    distance = dis;
                                    target   = targetCollector.Targets [i];
                                    if (weapon)
                                    {
                                        // random weapons
                                        indexWeapon = Random.Range(0, weapon.WeaponLists.Length);
                                    }
                                    timeAIattack = Time.time;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        void Update()
        {
            if (!flight)
            {
                return;
            }


            if (CenterOfBattle)
            {
                // CenterOfBattle is exist , BattlePosition = CenterOfBattle position
                //in case of able you to changing the battle area
                BattlePosition = CenterOfBattle.gameObject.transform.position;
            }

            // calculate target behavior.
            TargetBehaviorCal();



            // AI state machine
            switch (AIstate)
            {
            case AIState.Patrol:            // AI will free flying and looking for a target.
                for (int t = 0; t < TargetTag.Length; t++)
                {
                    if (AirStrike.AIPool == null)
                    {
                        Debug.LogError("Need AIManager placed in this scene, please go to AirStrike/Prefabs/Game/AIManager and placing AIManager prefab to the scene");
                        return;
                    }
                    TargetCollector targetCollector = AirStrike.AIPool.FindTargetTag(TargetTag [t]);


                    // Find only gameobject by Tag within TargetTag[]
                    if (targetCollector != null && targetCollector.Targets.Length > 0)
                    {
                        // get all objects as same tag as TargetTag[i]

                        float distance = int.MaxValue;
                        for (int i = 0; i < targetCollector.Targets.Length; i++)
                        {
                            if (targetCollector.Targets [i] != null)
                            {
                                // make it delay by TimeToLock
                                if (timetolockcount + TimeToLock < Time.time)
                                {
                                    float dis = Vector3.Distance(targetCollector.Targets [i].transform.position, transform.position);
                                    // selected closer target
                                    if (DistanceLock > dis)
                                    {
                                        if (!Target)
                                        {
                                            // i random a bit because i want this AI look hesitate. so you can remove "Random.Range (0, 100) > 80" if you want this AI select every target.
                                            if (distance > dis && Random.Range(0, 100) > 80)
                                            {
                                                distance            = dis;
                                                Target              = targetCollector.Targets [i];
                                                flight.FollowTarget = true;
                                                // go to Attacking state
                                                AIstate = AIState.Attacking;
                                                // save current time
                                                timestatetemp = Time.time;
                                                // random weapon from the flight.WeaponControl.WeaponLists
                                                WeaponSelected = Random.Range(0, flight.WeaponControl.WeaponLists.Length);
                                            }
                                        }
                                    }
                                }
                                // shooting..
                                shootTarget(targetCollector.Targets [i].transform.position);
                            }
                        }
                    }
                }
                break;

            case AIState.Idle:            // Free state
                // free fly and checking the AI is in of Battle Area
                if (Vector3.Distance(flight.PositionTarget, this.transform.position) <= FlyDistance)
                {
                    // go back to Patrol state
                    AIstate       = AIState.Patrol;
                    timestatetemp = Time.time;
                }

                break;

            case AIState.Attacking:            // Attacking state
                if (Target)
                {
                    // if target is exist , Position target = target position
                    flight.PositionTarget = Target.transform.position;
                    // shoot the target..
                    if (!shootTarget(flight.PositionTarget))
                    {
                        if (attacking)
                        {
                            // if in attacking but cannot shoot anymore turn back in 5 sec
                            if (Time.time > timestatetemp + 5)
                            {
                                turnPosition();
                            }
                        }
                        else
                        {
                            // if no attacking turn back in 7 sec
                            if (Time.time > timestatetemp + 10)
                            {
                                turnPosition();
                            }
                        }
                    }
                }
                else
                {
                    // if target is Destroyed go back to Patrol state
                    AIstate = AIState.Patrol;
                    // save current time
                    timestatetemp = Time.time;
                }
                // if going out of the Battle Area. Go back to the center (BattlePosition)
                if (Vector3.Distance(BattlePosition, this.transform.position) > FlyDistance)
                {
                    gotoCenter();
                }
                break;

            case AIState.TurnPosition:            // Turn back state
                if (Time.time > timestatetemp + 7)
                {
                    //try to turn to attacking again after 7 sec in case of Target still alive but you just turning out of shooting range
                    timestatetemp = Time.time;
                    AIstate       = AIState.Attacking;
                }
                // if going out of the Battle Area. Go back to the center (BattlePosition)
                if (Vector3.Distance(BattlePosition, this.transform.position) > FlyDistance)
                {
                    gotoCenter();
                }

                // current target height Y
                float height = flight.PositionTarget.y;
                if (targetHavior == TargetBehavior.Static)                  // if static target
                {
                    directionTurn.y = 0;
                    // calculate Position of bullet Drop and speed. try to make it accuracy
                    flight.PositionTarget   += (this.transform.forward + directionTurn) * flight.Speed;
                    flight.PositionTarget.y  = height;
                    flight.PositionTarget.y += flight.Speed / 2;
                }
                else
                {
                    // calculate Position of bullet Drop and speed. try to make it accuracy
                    flight.PositionTarget   += (this.transform.forward + directionTurn) * flight.Speed;
                    flight.PositionTarget.y  = height;
                    flight.PositionTarget.y += flight.Speed / 2;
                    //flight.PositionTarget = BattlePosition + new Vector3 (Random.Range (-FlyDistance, FlyDistance), Random.Range (0, FlyDistance / 2), Random.Range (-FlyDistance, FlyDistance));
                }
                break;
            }
            if (Avoidance)
            {
                Vector3 directavoid = avoidDirection();
                if (directavoid != Vector3.zero)
                {
                    flight.FollowTarget   = true;
                    flight.PositionTarget = this.transform.position + directavoid.normalized * AvoidChackDistance;
                    Debug.DrawLine(this.transform.position, flight.PositionTarget, Color.blue);
                }
            }
        }