Esempio n. 1
0
    void Update()
    {
        if (Pause)
        {
            rigidbody2D.Sleep();
            return;
        }
        else if (!Pause && rigidbody2D.IsSleeping())
        {
            rigidbody2D.WakeUp();
        }

        if (move)
        {
            if (Input.touchCount > 0 && Time.time > nextFire)
            {
                nextFire = Time.time + fireRate;
                Vector2 direction = (Input.GetTouch(0).position - new Vector2(Screen.width / 2, 0)).normalized;

                GameObject   obj    = (GameObject)Instantiate(bullet, new Vector2(transform.position.x, transform.position.y + renderer.bounds.size.y - bullet.renderer.bounds.size.y), Quaternion.identity);
                BulletScript script = obj.GetComponent <BulletScript>();
                script.setDirection(direction);
                script.Move();
            }

            if (Application.platform != RuntimePlatform.Android)
            {
                if (Input.GetButtonDown("Fire1"))
                {
                    nextFire = Time.time + fireRate;
                    Vector3 direction = (Input.mousePosition - new Vector3(Screen.width / 2, 0)).normalized;

                    GameObject   obj    = (GameObject)Instantiate(bullet, new Vector2(transform.position.x, transform.position.y + renderer.bounds.size.y - bullet.renderer.bounds.size.y), Quaternion.identity);
                    BulletScript script = obj.GetComponent <BulletScript>();
                    script.setDirection(direction);
                    script.Move();
                }
            }
        }
    }
Esempio n. 2
0
    void Update()
    {
        if (Pause)
        {
            rigidbody2D.Sleep();
            return;
        }
        else if (!Pause && rigidbody2D.IsSleeping())
        {
            rigidbody2D.WakeUp();
        }

        if (move)
        {
            if (indestructible && Time.time - powerupTimer > 15f)
            {
                indestructible = false;
                transform.GetChild(0).gameObject.layer = enemyLayer;
            }
            else if (supershooter && Time.time - powerupTimer > 10f)
            {
                supershooter = false;
                fireRate     = 0.5f;
            }
            else if (slowdown && Time.time - powerupTimer > 10f)
            {
                slowdown       = false;
                Time.timeScale = 1.0f;
            }

            if (gunRotReset != 0)
            {
                if (Time.time - gunRotReset > 0.5f)
                {
                    resetGun();
                }
            }

            if (Input.touchCount > 0 && Time.time > nextFire)
            {
                Rect rect  = new Rect(Screen.width / 2 - 100, Screen.height - 50, 200, 60);
                Rect rect2 = new Rect(Screen.width - 44, 4, 40, 40);

                if ((hud.showGUI && rect.Contains(new Vector2(Input.GetTouch(0).position.x, Screen.height - Input.GetTouch(0).position.y))) || rect2.Contains(new Vector2(Input.GetTouch(0).position.x, Screen.height - Input.GetTouch(0).position.y)))
                {
                    return;
                }

                GameObject  shootSound = GameObject.Find("Sounds").transform.Find("Shoot").gameObject;
                AudioSource ac         = shootSound.GetComponent <AudioSource>();
                ac.audio.Play();

                gunRotReset = Time.time;
                nextFire    = Time.time + fireRate;
                Vector2 direction = (Input.GetTouch(0).position - new Vector2(Screen.width / 2, 0)).normalized;
                direction = new Vector2(Mathf.Clamp(direction.x, -5, 5), Mathf.Clamp(direction.y, 1, 5));

                var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

                Quaternion rot = Quaternion.AngleAxis(angle - 32, Vector3.forward);

                if (rot.z > 0.45 && !inverted)
                {
                    inverted       = true;
                    gun.localScale = new Vector3(-gun.localScale.x, gun.localScale.y, gun.localScale.z);
                }
                else if (rot.z < 0.45 && inverted)
                {
                    inverted       = false;
                    gun.localScale = new Vector3(-gun.localScale.x, gun.localScale.y, gun.localScale.z);
                }

                if (inverted)
                {
                    rot = Quaternion.AngleAxis(angle + 32 + 180, Vector3.forward);
                }

                gun.rotation = rot;

                GameObject   obj    = (GameObject)Instantiate(bullet, new Vector2(gun.position.x, gun.position.y), Quaternion.identity);
                BulletScript script = obj.GetComponent <BulletScript>();
                script.setDirection(direction);
                script.Move();
            }

            if (Application.platform != RuntimePlatform.Android)
            {
                if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
                {
                    Rect rect  = new Rect(Screen.width / 2 - 100, Screen.height - 50, 200, 60);
                    Rect rect2 = new Rect(Screen.width - 44, 4, 40, 40);

                    if ((hud.showGUI && rect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) || rect2.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y)))
                    {
                        return;
                    }

                    GameObject  shootSound = GameObject.Find("Sounds").transform.Find("Shoot").gameObject;
                    AudioSource ac         = shootSound.GetComponent <AudioSource>();
                    ac.audio.Play();

                    gunRotReset = Time.time;
                    nextFire    = Time.time + fireRate;
                    Vector3 direction = (Input.mousePosition - new Vector3(Screen.width / 2, 0)).normalized;
                    direction = new Vector2(Mathf.Clamp(direction.x, -5, 5), Mathf.Clamp(direction.y, 1, 5));

                    var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

                    Quaternion rot = Quaternion.AngleAxis(angle - 32, Vector3.forward);

                    if (rot.z > 0.45 && !inverted)
                    {
                        inverted       = true;
                        gun.localScale = new Vector3(-gun.localScale.x, gun.localScale.y, gun.localScale.z);
                    }
                    else if (rot.z < 0.45 && inverted)
                    {
                        inverted       = false;
                        gun.localScale = new Vector3(-gun.localScale.x, gun.localScale.y, gun.localScale.z);
                    }

                    if (inverted)
                    {
                        rot = Quaternion.AngleAxis(angle + 32 + 180, Vector3.forward);
                    }

                    gun.rotation = rot;

                    GameObject   obj    = (GameObject)Instantiate(bullet, new Vector2(gun.position.x, gun.position.y), Quaternion.identity);
                    BulletScript script = obj.GetComponent <BulletScript>();
                    script.setDirection(direction);
                    script.Move();
                }
            }
        }
    }