Exemplo n.º 1
0
    private void Update()
    {
        if (isFind)
        {
            float             oldDistance   = 1000f;
            SoldierController _targetContrl = null;
            foreach (Collider col in Physics.OverlapSphere(transform.position, SoldierInterface.DetectionRadius))
            {
                if (col.tag == enemyTeamTag.ToString() && col.transform.GetChild(0).GetComponent <SoldierController>().IsAlive)
                {
                    var currentDistance = Vector3.Distance(col.transform.position, GetPositions());
                    if (oldDistance > currentDistance)
                    {
                        oldDistance = currentDistance; _targetContrl = col.transform.GetChild(0).GetComponent <SoldierController>();
                    }
                }
            }
            if (_targetContrl != null)
            {
                isFind       = false;
                targetContrl = _targetContrl;
                AttackInterface.AttackTarget(targetContrl);
            }
        }

        if (isMove && isMoveToTarget && targetContrl != null)
        {
            if (Vector3.Distance(GetPositions(), TargetContr.GetPositions()) <= SoldierInterface.AttackRange)
            {
                isMove         = false;
                isMoveToTarget = false;
                agent.Stop();
                agent.ResetPath();
                AttackInterface.AttackTarget(targetContrl);
            }
            else
            {
                MoveInterface.MoveTo(TargetContr.GetPositions());
            }
        }

        if (isMove && !isMoveToTarget)
        {
            if (agent.remainingDistance <= agent.stoppingDistance)
            {
                isMove = false;
                isFind = true;
            }
        }

        if (targetContrl != null)
        {
            Vector3 lookPos = targetContrl.GetPositions() - GetPositions();
            if (lookPos != Vector3.zero)
            {
                Quaternion rotation = Quaternion.LookRotation(lookPos);
                myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rotation, Time.deltaTime * SoldierInterface.RotationSpeed);
            }
        }
    }
        public bool TryInteractWithObject(MonoBehaviour monoBehaviourObj)
        {
            if (!_isInit)
            {
                return(false);
            }

            var damageable = (IDamageable)monoBehaviourObj;
            var targetable = (ITargetable)monoBehaviourObj;

            if (damageable != null && targetable != null)
            {
                if (targetable.IsEnemy(_targetable.TeamId) && damageable.CanBeDamaged())
                {
                    _attackable.AttackTarget(targetable);
                    return(true);
                }
            }

            return(false);
        }