Example #1
0
 /// <summary>
 /// Actualiza su estado dependiendo de si ha visto al jugador o está cerca de él
 /// </summary>
 public override void Sight(RaycastHit2D sight)
 {
     base.Sight(sight);
     laserHitPos = sight.point;
     if (playerDetected)
     {
         if (Vector2.Distance(player.transform.position, transform.position) < minDistance)
         {
             state = EnemyState.Fleeing;
             followDirection.Stop();
             lr.enabled = false;
             shooting.ResetCooldown();
         }
         else
         {
             state      = EnemyState.Shooting;
             lr.enabled = playerDetected;
             followDirection.Stop();
         }
     }
     else
     {
         state = EnemyState.Idle;
         followDirection.Stop();
         lr.enabled = false;
     }
 }
    /// <summary>
    /// Si está en fleeing, se mueve contrario al jugador, si no, se mueve hacia él
    /// Actualiza el tiempo de melee
    /// Dispara
    /// </summary>
    private void Update()
    {
        if (!stunned)
        {
            switch (state)
            {
            case EnemyState.Fleeing:
                moveDir = (transform.position - player.transform.position).normalized;
                break;

            case EnemyState.Chasing:
                moveDir = (player.transform.position - transform.position).normalized;
                break;

            case EnemyState.Idle:
                follow.Stop();
                break;
            }
            //Se suma avoidDir para hacer un movimiento compuesto
            moveDir += avoidDir;
            if (moveDir.magnitude > 0)
            {
                follow.MoveTowards(moveDir);
            }
            else
            {
                follow.Stop();
            }

            moveDir  = Vector2.zero;
            avoidDir = Vector2.zero;

            Cooldown(ref meleeTimer);

            shooting.Cooldown();
            shooting.Shoot();
        }
        else
        {
            follow.Stop();
        }
    }
    private void Update()
    {
        switch (state)
        {
        case EnemyState.Chasing:
            anim.SetFloat("Distance", Vector3.Distance(transform.position, playerPos));
            break;

        case EnemyState.Stunned:
            follow.Stop();
            break;
        }
    }