Пример #1
0
    public override void PerformBehavior()
    {
        timer += Time.deltaTime;
        if (targetedLayer != 1 << 12)
        {
            if (timer >= 1)
            {
                Collider[] colliders = Physics.OverlapSphere(transform.position, lookRadius, targetedLayer);
                if (colliders.Length > 0)
                {
                    float     shortestSqrDistance = Mathf.Infinity;
                    Transform nearestTarget       = null;
                    Vector3   currentLoction      = transform.position;

                    foreach (Collider collider in colliders)
                    {
                        if (collider.GetComponent <CharacterStats>() && !collider.GetComponent <CharacterStats>().IsDead)
                        {
                            float sqrDistanceToTarget = (currentLoction - collider.transform.position).sqrMagnitude;
                            if (sqrDistanceToTarget < shortestSqrDistance)
                            {
                                shortestSqrDistance = sqrDistanceToTarget;
                                nearestTarget       = collider.transform;
                            }
                        }
                    }

                    if (nearestTarget.GetComponent <CharacterStats>())
                    {
                        npcController.SetTarget(nearestTarget);
                        npcController.SetState(npcController.GetChaseState());
                    }
                }

                timer = 0;
            }
        }

        if (!rotating && !agent.hasPath)
        {
            agent.SetDestination(startingLocation);
        }

        if (!rotating && !agent.pathPending)
        {
            if (agent.remainingDistance <= agent.stoppingDistance)
            {
                if (!agent.hasPath || agent.velocity.sqrMagnitude == 0f)
                {
                    rotating = true;
                }
            }
        }

        if (rotating)
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, initialRotation, Time.deltaTime * 5);
            if (Quaternion.Angle(transform.rotation, initialRotation) <= 0.25)
            {
                rotating = false;
                npcController.ClearThreatTable();
                npcController.SetState(npcController.GetSearchingState());
            }
        }
    }