Пример #1
0
    private void Start()
    {
        //Transform vectors to world space
        for (int i = 0; i < patrolPoints.Length; i++)
        {
            patrolPoints[i] += transform.position;
        }

        beganPlay = true;

        pawn  = GetComponent <Enemy>();
        agent = GetComponent <PathfindingAgent>();
        agent.MoveTowards(patrolPoints[currentTargetIndex]);
    }
    void Update()
    {
        //Player within detection radius
        float dist = (target.transform.position - transform.position).magnitude;

        if (dist <= detectionRadius)
        {
            /*if (engageOnlyIfVisible)
             * {
             *      RaycastHit2D hit = Physics2D.Raycast(transform.position, target.transform.position, detectionRadius);
             *
             *      //First object hit is not our target
             *      if (hit.collider.gameObject != target.gameObject)
             *              return;
             * }*/

            if (!playerDetected)
            {
                playerDetected = true;
                if (OnPlayerDetected != null)
                {
                    OnPlayerDetected();
                }
            }

            //Player within attack radius
            if ((dist <= attackRadius) && currentAttackCD <= 0)
            {
                currentAttackCD = pawn.cooldown;

                pawn.LookAt(target.transform.position);
                pawn.MeleeAttack();
                pawnAgent.Stop();
                pursuing = false;
            }

            //Adjust path only if we can attack
            if (currentReadjustCD <= 0 && currentAttackCD <= 0)
            {
                pursuing = true;
                Vector3 target = GetNearestAdyacent();
                pawnAgent.MoveTowards(target);
                currentReadjustCD = pathReadjustCooldown;
            }
        }
        else if (playerDetected)
        {
            playerDetected = false;
            pursuing       = false;
            pawnAgent.Stop();

            if (OnPlayerUnDetected != null)
            {
                OnPlayerUnDetected();
            }
        }

        //Attack cooldown
        if (currentAttackCD > 0)
        {
            currentAttackCD -= Time.deltaTime;
        }

        //Readjustment cooldown
        if (currentReadjustCD > 0)
        {
            currentReadjustCD -= Time.deltaTime;
        }
    }
Пример #3
0
    private void HandleMovement(float deltaTime)
    {
        Vector3 inputDirection = HandleMovementInput();

        pathfindingAgent.MoveTowards(Position + inputDirection * speed, this);
    }