private void onInit()
    {
        // EARLY OUT! //
        if (_entity == null)
        {
            return;
        }

        var owner = _entity.Owner;

        if (owner != null && owner.HQ != null)
        {
            var fireTransform = owner.HQ.transform.Find("FireTransform");

            if (fireTransform != null)
            {
                // Fire a projectile to this position.
                var projectile = CombatUtils.FireProjectile(_entity,
                                                            _projectile,
                                                            fireTransform,
                                                            transform.position,
                                                            _secondsToFlyHorizontalMeter);

                if (projectile != null)
                {
                    // Change the clip to the firing clip and play it.
                    _shootingAudio.clip = _fireClip;
                    _shootingAudio.Play();

                    projectile.DestroyedEvent.AddListener(onProjectileDestroyed);
                }
            }
        }
    }
Exemplo n.º 2
0
    private void Update()
    {
        // Our code which does simple shooting AI on cooldown if we have a target.
        _cooldownSeconds -= Time.deltaTime;

        var aggroTarget = _aggro.Target;

        if (aggroTarget != null && _cooldownSeconds <= 0f)
        {
            float distance = Vector3.Distance(transform.position, aggroTarget.transform.position);

            if (distance < _entity.AttackRange)
            {
                if (_aggro.IsInSights(aggroTarget.transform, _isDirectional))
                {
                    _cooldownSeconds = _entity.AttackSpeed;

                    CombatUtils.FireProjectile(_entity,
                                               _shell,
                                               _fireTransform,
                                               aggroTarget.transform.position,
                                               _secondsToFlyHorizontalMeter);

                    // Apply recoil to this entity for firing.
                    if (_projectileRecoil > 0f)
                    {
                        _rigidbody.AddForce(-transform.forward * _projectileRecoil, ForceMode.VelocityChange);
                    }

                    if (_shootingAudio != null)
                    {
                        // Change the clip to the firing clip and play it.
                        _shootingAudio.clip = _fireClip;
                        _shootingAudio.Play();
                    }
                }
            }
        }
    }