Exemple #1
0
    // Update is called once per frame
    void Update()
    {
        if (!deathScript.isAlive())
        {
            var spriteRenderers = gameObject.GetComponentsInChildren <SpriteRenderer>();
            gameObject.GetComponent <BoxCollider2D>().enabled = false;

            foreach (SpriteRenderer sr in spriteRenderers)
            {
                sr.enabled = false;
            }

            return;
        }


        var distanceToPlayer = (transform.position - player.transform.position).magnitude;

        if (player.transform.position.x > transform.position.x)
        {
            transform.localScale = startScale;
        }
        else
        {
            transform.localScale = new Vector2(-startScale.x, startScale.y);
        }

        //Debug.Log(distanceToPlayer);

        if (distanceToPlayer < aggroDistance && ticker < 0)
        {
            var direction = (player.transform.position - transform.position).normalized;

            Instantiate <Rigidbody2D>(projectile, transform.position, transform.rotation).AddForce(direction * projectileSpeed);

            ticker = reloadTimeInSeconds;
        }
        ;

        ticker -= Time.deltaTime;
    }
Exemple #2
0
    void Update()
    {
        if (!deathScript.isAlive())
        {
            var spriteRenderers = gameObject.GetComponentsInChildren <SpriteRenderer>();
            gameObject.GetComponent <BoxCollider2D>().enabled = false;
            foreach (SpriteRenderer sr in spriteRenderers)
            {
                sr.enabled = false;
            }

            return;
        }

        var distanceToPlayer = (transform.position - player.transform.position).magnitude;

        // * Chase
        if (distanceToPlayer < aggroDistance)
        {
            var direction = (player.transform.position - transform.position).normalized;

            rBody2d.velocity = chasingSpeed * direction * Time.deltaTime;

            chasedLastFrame = true;
        }
        else if (movingEnabled)            // * Patrol

        {
            if (chasedLastFrame)
            {
                origoPosition       = transform.position;
                targetLocation      = RandomVector2D() + (Vector2)transform.position;
                movingTowardsTarget = true;

                movingEnabled = false;
                Invoke("EnableMoving", 1f);
            }

            if (movingTowardsTarget)
            {
                var distanceToTargetLocation = ((Vector2)transform.position - targetLocation).magnitude;
                var direction = (targetLocation - (Vector2)transform.position).normalized;

                if (distanceToTargetLocation < 0.3f)
                {
                    movingTowardsTarget = false;
                    movingEnabled       = false;
                    Invoke("EnableMoving", 1f);
                }
                else
                {
                    rBody2d.velocity = patrollingSpeed * direction * Time.deltaTime;
                }
            }
            else
            {
                var distanceToOrigo = ((Vector2)transform.position - origoPosition).magnitude;
                var direction       = (origoPosition - (Vector2)transform.position).normalized;

                if (distanceToOrigo < 0.3f)
                {
                    targetLocation      = RandomVector2D() + (Vector2)transform.position;
                    movingTowardsTarget = true;
                    movingEnabled       = false;
                    Invoke("EnableMoving", 1f);
                }
                else
                {
                    rBody2d.velocity = patrollingSpeed * direction * Time.deltaTime;
                }
            }

            chasedLastFrame = false;
        }
    }