Beispiel #1
0
    private bool attemptDirectAttack(Entity target)
    {
        bool didAttack = false;

        if (target != null && _entity.DirectAttackDamage > 0)
        {
            if (target != null)
            {
                // If the entity needs to aim, we have to wait until the target is in our sights.
                // If the direction doesn't matter, we should be ready to fire.
                if (_aggro.IsInSights(target.transform, _isDirectional))
                {
                    attack(target, _entity.DirectAttackDamage);
                    didAttack = true;
                }
            }
        }
        return(didAttack);
    }
    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();
                    }
                }
            }
        }
    }
Beispiel #3
0
    // This looks a whole lot like tank shooting.
    // TODO: Reduce code duplication.
    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)
        {
            var targetPositionIgnoreY = aggroTarget.transform.position;
            targetPositionIgnoreY.y = transform.position.y;

            // Fire a shot if we are close enough and pointing approximately at the target.
            var distance = Vector3.Distance(transform.position, targetPositionIgnoreY);

            bool isInSights = _aggro.IsInSights(aggroTarget.transform, _isDirectional);
            if (distance < _entity.AttackRange && isInSights)
            {
                _cooldownSeconds = _entity.AttackSpeed;
                fire(aggroTarget);
            }
        }
    }