// Update is called once per frame
 void Update()
 {
     if (Input.GetButtonDown("Fire1") && _gun)
     {
         _gun.Fire(transform.up * _shotSpeed);
     }
 }
    void Update()
    {
        UpdatePosition();
        UpdateRotation();

        if (Input.GetMouseButton(0))
        {
            _gun.Fire();
        }
    }
Beispiel #3
0
    void Update()
    {
        if ((_lastKnownPlayerPosition - _player.position).sqrMagnitude > 1.0f)
        {
            _lastKnownPlayerPosition = _player.position;
            _agent.SetDestination(_lastKnownPlayerPosition);
        }

        if (IsPlayerVisibile())
        {
            _gun.Fire();
            _agent.isStopped = true;

            var desiredRot = Quaternion.LookRotation(_player.position - transform.position);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredRot, Time.deltaTime * _agent.angularSpeed);
        }
        else
        {
            _agent.isStopped = false;
        }
    }
    private void Update()
    {
        Debug.DrawLine(transform.position, _agent.destination, Color.magenta);

        if (_state == BuddyState.Healing)
        {
            if (!_playerInfo.IsCriticalHealth)
            {
                _state = BuddyState.Hiding;
                OnStateChangeListener?.Invoke(_state);
            }
            else
            {
                if ((_player.position - _lastTargetPlayerPosition).sqrMagnitude > 0.1f)
                {
                    _lastTargetPlayerPosition = _player.position;
                    _agent.SetDestination(_lastTargetPlayerPosition);
                }

                var distSq = (_player.position - transform.position).sqrMagnitude;
                if (distSq < minDistanceToPlayer * minDistanceToPlayer)
                {
                    var playerHealth = _player.GetComponent <HealthBehavior>();
                    playerHealth.health += healAmount;

                    FindNewPosition();
                }
            }
        }
        else if (_state == BuddyState.Attacking)
        {
            if (_playerInfo.IsCriticalHealth)
            {
                _state = BuddyState.Healing;
                OnStateChangeListener?.Invoke(_state);
                _lastTargetPlayerPosition = _player.position;
                _agent.SetDestination(_lastTargetPlayerPosition);
                _agent.updateRotation = true;
            }
            else if (_targetEnemy != null && IsVisibile(_targetEnemy.position))
            {
                var desiredRot = Quaternion.LookRotation(_targetEnemy.position - transform.position);
                var angle      = Quaternion.Angle(transform.rotation, desiredRot);
                transform.rotation = Quaternion.RotateTowards(transform.rotation, desiredRot, _agent.angularSpeed * Time.deltaTime);

                if (angle < 5.0f)
                {
                    _gun.Fire();
                }

                if (_agent.remainingDistance < 0.2f)
                {
                    FindNewPosition(false);
                }
            }
            else
            {
                _state = BuddyState.Hiding;
                OnStateChangeListener?.Invoke(_state);
                _agent.updateRotation = true;
            }
        }
        else if (_state == BuddyState.Hiding)
        {
            if (_playerInfo.IsCriticalHealth)
            {
                _state = BuddyState.Healing;
                OnStateChangeListener?.Invoke(_state);
                _lastTargetPlayerPosition = _player.position;
                _agent.SetDestination(_lastTargetPlayerPosition);
            }
            else
            {
                var visibleEnemies = GetVisibileEnemies();
                if (visibleEnemies.Count > 0)
                {
                    _state = BuddyState.Attacking;
                    OnStateChangeListener?.Invoke(_state);
                    _targetEnemy          = FindEnemyToAttack(visibleEnemies).transform;
                    _agent.updateRotation = false;
                }

                if (!IsWithinProximityOfPlayer(transform.position))
                {
                    _lastTargetPlayerPosition = _player.position;
                    FindNewPosition();
                }
            }
        }
    }