4

Olá. Estou desenvolvendo um jogo em que o player atira algumas bolinhas(tiros), então criei um powerup que diminui o tempo entre as bolinhas atiradas. Eu preciso de fazer com o tempo entre as bolinhas volte ao normal após um determinado tempo.

void Update()
{
    cax = GameObject.FindGameObjectWithTag ("PowerUp");

    if(Input.GetButton("Fire1") && Time.time >= nextFire)
    {
        Fire();
        nextFire = Time.time + timeBetweenBullets;
    }
}

// Capturar o 
//POWERUP
void OnTriggerEnter(Collider other)
        {   
            if(other.gameObject == cax)
            {
                timeBetweenBullets = 0.10f;
                Destroy(cax);
            }
        }

void Fire()
{
    //BULLET
    //1

    // Create the Bullet from the Bullet Prefab
        var bullet1 = (GameObject)Instantiate(
        bulletPrefab,
        bulletSpawn.position,
        bulletSpawn.rotation);

    // Add velocity to the bullet
    bullet1.GetComponent<Rigidbody>().velocity = bullet1.transform.forward * 50;
    GetComponent<AudioSource>().clip = Tiro;
    GetComponent<AudioSource>().Play ();
    // Destroy the bullet after 2 seconds
    Destroy(bullet1, 2.0f);        

    //BULLET
    //2


            // Create the Bullet from the Bullet Prefab
        var bullet2 = (GameObject)Instantiate(
        bulletPrefabs,
        bulletSpawns.position,
        bulletSpawns.rotation);

    // Add velocity to the bullet
    bullet2.GetComponent<Rigidbody>().velocity = bullet2.transform.forward * 50;
    GetComponent<AudioSource>().clip = Tiro;
    GetComponent<AudioSource>().Play ();

    // Destroy the bullet after 2 seconds
    Destroy(bullet2, 2.0f);     
}

1 Resposta 1

4

Você pode manter um segundo atributo privado da classe para "contar" o tempo decorrido da mesma forma como vc conta nextFire. Esse segundo tempo seria contado até o valor que vc configurar para o tempo de término do power-up e aí você voltaria o valor de timeBetweenBullets para o original.

Entretanto, uma alternativa que eu acho mais elegante é você ter um método privado que retorna o valor de timeBetweenBullets para o original e invocá-lo após o tempo configurado utilizando uma chamada de Invoke. Por exemplo:

void resetPowerup()
{
    timeBetweenBullets = 1f; // Supondo que o valor original era 1
}

void OnTriggerEnter(Collider other)
    {   
        if(other.gameObject == cax)
        {
            timeBetweenBullets = 0.10f;
            Destroy(cax);

            //++++++++++++++++++++
            Invoke("resetPowerup", 10); // Invoca "resetPowerup" após 10s
            //++++++++++++++++++++
        }
    }

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .