Time Between Shots

I have done everything same as Rick but I have to keep min and max time between shots very high as of 20 and 30 otherwise the projectiles are shooting very fastly.
This may sound a bit silly but is it okay or I am wrong somewhere??

Hmm I’d say your counter isn’t working correctly (possibly something to do with it not resetting after it gets to 0, or a missed time.Deltatime somewhere). Maybe post your code here or double/triple check it against the video especially where you’re setting the min/max values & timer code.

1 Like

Maybe you are right as now I am using 140 and 150 as min and max.
This is my code.

void Start()
    {
        shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
    }

    void Update()
    {
        CountDownAndShoot();
    }

    private void CountDownAndShoot()
    {
        shotCounter -= Time.timeScale;
        if(shotCounter <= 0)
        {
            Fire();
            shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
        }
    }

    private void Fire()
    {
        Vector2 enemyCannonBallSpawnPos = new Vector2(transform.position.x, transform.position.y - cannonBallSpawnDist);
        GameObject enemyCannonBall = Instantiate(cannonBall,
                                                 enemyCannonBallSpawnPos,
                                                 transform.rotation) as GameObject;
        enemyCannonBall.GetComponent<Rigidbody2D>().rotation += 180;
        enemyCannonBall.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -cannonBallSpeed);
    }

This line here: shotCounter -= Time.timeScale;

should be time.deltaTime instead.

Thanks!! Now it is ok.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms