Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        GameObject tankGameObject = GameObject.Find("Tank");
        Vector3    startPos       = transform.position;
        float      rotationBefore = transform.eulerAngles.z;

        transform.LookAt(tankGameObject.transform, Vector3.back);
        transform.Rotate(0, 0, 90);
        transform.eulerAngles = new Vector3(0, 0, -transform.eulerAngles.z); // TRUST ME
        Vector3 direction = transform.right;
        float   distance  = 5000000;

        Debug.DrawRay(startPos, direction);
        RaycastHit2D hit = Physics2D.Raycast(startPos, direction, distance);

        // If it hits something AND its a tank
        if (hit.collider.gameObject != null && hit.collider.tag == "Tank")
        {
            if (curCooldown <= 0)
            {
                spawnBulletScript.SpawnTheBullet(gameObject, hit.collider.transform.position, bulletSpeed);
                curCooldown = cooldown;
            }
            else
            {
                curCooldown -= Time.deltaTime;
            }
            GetComponent <Rigidbody2D>().velocity = transform.up * speed * Time.deltaTime;
        }
        else
        {
            transform.eulerAngles = new Vector3(0, 0, rotationBefore);
        }
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        if (!waitForStartScript.HasStarted())
        {
            return;
        }
        if (checkModeScript.modeChanged())
        {
            return;
        }
        if (Input.GetMouseButtonUp((int)MouseButton.LeftMouse))
        {
            mouseUp = true;
        }
        Scene scene = SceneManager.GetActiveScene();

        // If on the last level, stop shooting
        if (scene.name == "GameScene6")
        {
            return;
        }

        if (currentCooldown <= 0 && mouseUp && !endGameScript.IsDead())
        {
            if (Input.GetMouseButton((int)MouseButton.LeftMouse))
            {
                GameObject tankGameObject = GameObject.Find("Tank");
                Vector3    cursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                spawnBulletScript.SpawnTheBullet(tankGameObject, cursorPosition, bulletSpeed);
                AudioSource.PlayClipAtPoint(Resources.Load <AudioClip>("GameScene/Audio/shoot"), new Vector3(0, 0, 0));

                currentCooldown = cooldown;
            }
        }
        else
        {
            currentCooldown -= Time.deltaTime;
        }
    }