Exemplo n.º 1
0
    void Update()
    {
        if (projectile != null && !LevelManager.instance.paused && sr.isVisible)
        {
            shootTimer += Time.deltaTime;

            if (shootTimer >= SHOOT_COOLDOWN)
            {
                // Find closest player and shoot at them.
                GameObject player = AIUtilities.getClosestPlayer(transform.position);

                // only shoot if player is approximately on our y-level
                if (Mathf.Abs(player.transform.position.y - transform.position.y) < 2f)
                {
                    // shoot
                    float       direction     = Mathf.Sign(player.transform.position.x - transform.position.x);
                    GameObject  newProjectile = LevelManager.instance.placeSpawnedObject((Vector2)gameObject.transform.position + new Vector2(direction, 0f), projectile, transform.parent);
                    Rigidbody2D rb            = newProjectile.GetComponent <Rigidbody2D>();
                    if (rb != null)
                    {
                        rb.velocity = new Vector2(projectileSpeed * direction, rb.velocity.y);
                    }
                    Vector3 scale = newProjectile.transform.localScale;
                    scale.x = Mathf.Abs(scale.x) * direction;
                    newProjectile.transform.localScale = scale;
                    shootTimer = 0f;
                }
            }
        }
    }
Exemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        if (sr == null)
        {
            sr = GetComponent <SpriteRenderer>();
        }

        if (!sr.isVisible)
        {
            return;
        }

        if (rb == null)
        {
            rb = GetComponent <Rigidbody2D>();
        }

        // Find closest player and move toward it
        GameObject player = AIUtilities.getClosestPlayer(transform.position);

        if (player != null && rb != null)
        {
            float direction = Mathf.Sign(player.transform.position.x - transform.position.x);

            Vector2 position = transform.position;
            position.x        += (direction * moveSpeed) * Time.deltaTime;
            transform.position = position;
            Vector3 scale = transform.localScale;
            scale.x = Mathf.Abs(scale.x) * -direction;
            transform.localScale = scale;
        }
    }