Example #1
0
    void FixedUpdate()
    {
        float        dist      = Vector3.Distance(transform.position, player.position);
        Vector3      direction = player.position - transform.position;
        RaycastHit2D hit       = Physics2D.Raycast(transform.position, direction);

        if (dist > atkDist || hit.collider == null || hit.collider.tag != "Player")
        {
            laser.stop();

            findPathToPlayer();

            if (currentPath != null)
            {
                Vector2 accel = followPath.getSteering(currentPath);
                steeringUtils.steer(accel);
                steeringUtils.lookWhereYoureGoing();
            }
            // If we have not path to the player then stand still
            else
            {
                rb.velocity = Vector2.zero;
            }
        }
        else
        {
            laser.fire(hit);

            rb.velocity = Vector2.zero;
            steeringUtils.lookAtDirection(direction);
        }
    }
Example #2
0
    void FixedUpdate()
    {
        rb.constraints = RigidbodyConstraints2D.FreezeAll;

        if (target != null && target is Hero)
        {
            findPathToHero();
        }

        // Follow path and atk target if its an enemy
        if (currentPath != null)
        {
            if (target != null && atkRange.targets.Contains(target.transform))
            {
                //Look at the target and stop moving
                steeringUtils.lookAtDirection(target.transform.position - transform.position);
                rb.velocity = Vector2.zero;

                if (enemyHealth != null && Time.time > nextFire)
                {
                    nextFire = Time.time + atkRate;
                    Transform clone = Instantiate(bullet, transform.position, Quaternion.identity) as Transform;
                    clone.GetComponent <Bullet> ().setUp(enemyHealth, atkDmg);
                }
            }
            else if (!isLoopingPath() && (isAtEndOfPath() || (Time.time - farthestPathTime) > repathTimeout))
            {
                if (!isAtEndOfPath())
                {
                    setArriveTarget();
                }

                currentPath = null;
            }
            else
            {
                moveAlongPath();
            }
        }
        else if (arriveStartTime + arriveAfterPathTimeout > Time.time)
        {
            moveToTarget();
        }
        // If we have no path to the player then stand still
        else
        {
            rb.velocity = Vector2.zero;
        }
    }
Example #3
0
    public void tryToAttack()
    {
        if (enemyHealth != null && diagonalDist(reservedPos, target.reservedPos) <= distToTarget)
        {
            //Look at the target and stop moving
            steeringUtils.lookAtDirection(target.transform.position - transform.position);

            if (Time.time > nextFire)
            {
                if (shootSound != null)
                {
                    AudioSource.PlayClipAtPoint(shootSound, Camera.main.transform.position, shootVolume);
                }

                nextFire = Time.time + atkRate;
                Transform clone = Instantiate(bullet, transform.position + Bullet.aboveGround, Quaternion.identity) as Transform;
                clone.GetComponent <Bullet> ().setUp(enemyHealth, atkDmg);
            }
        }
    }