Example #1
0
        IEnumerator AttackState(GameObject ToAttack)
        {
            Steering.DestinationTransform = null;
            Steering.FaceTowardsTransform = ToAttack.transform;
            var alarmTimer = SoundAlarmTime;

Start:

            if (ToAttack == null)
            {
                StartCoroutine(PauseState());
                yield break;
            }

            alarmTimer -= Time.deltaTime;
            if (alarmTimer <= 0f)
            {
                AlarmController.Instance.StartAlarm(ToAttack);
            }

            if (!Sight.IsDetected(ToAttack))
            {
                Steering.FaceTowardsTransform    = null;
                GunPivot.transform.localRotation = Quaternion.identity; // Return gun rotation back to resting position
                StartCoroutine(Investigate(ToAttack.transform.position));
                yield break;
            }

            // Roate the gun in hand to face the enemy, reload if empty, otherwise fire the gun.
            GunPivot.transform.LookAt(new Vector3(ToAttack.transform.position.x, GunPivot.transform.position.y, ToAttack.transform.position.z));
            if (gun.IsEmptyClip)
            {
                gun.Reload();
            }
            else
            {
                gun.Fire();
            }

            yield return(null);

            goto Start;
        }
Example #2
0
        IEnumerator AttackState(GameObject target = null)
        {
            if (target == null)
            {
                var enemies = enemiesSpotted;
                if (enemies.Count == 0)
                {
                    StartCoroutine(DefaultState()); yield break;
                }
                target = enemies[0];
            }

            var cooldown = Random.Range(0.5f, 2f);

            if ((target.transform.position - transform.position).magnitude > 10f)
            {
                // Charge
                while (cooldown > 0f)
                {
                    if (target == null)
                    {
                        break;
                    }

                    var targetDirection = (target.transform.position - transform.position).normalized;
                    movement.Move = SteerSensor.GetSteeredDirection(targetDirection);
                    movement.Face = targetDirection;

                    gun.Fire();
                    if (gun.IsEmptyClip)
                    {
                        gun.Reload();
                    }
                    cooldown -= Time.deltaTime;
                    yield return(null);
                }
            }
            else
            {
                // Strafe
                var strafeDirection = new Vector3(Random.Range(-1f, 1f), 0f, Random.Range(-1f, 1f)).normalized;
                while (cooldown > 0f)
                {
                    if (target == null)
                    {
                        break;
                    }

                    var targetDirection = (target.transform.position - transform.position).normalized;
                    movement.Move = SteerSensor.GetSteeredDirection(strafeDirection);
                    movement.Face = targetDirection;

                    gun.Fire();
                    if (gun.IsEmptyClip && !gun.IsReloading)
                    {
                        gun.Reload();
                        if (Random.value > 0.5f)
                        {
                            StartCoroutine(FleeState()); yield break;
                        }
                    }
                    cooldown -= Time.deltaTime;
                    yield return(null);
                }
            }

            StartCoroutine(DefaultState());
        }