Question by
MBAmissah · Sep 11, 2016 at 04:36 AM ·
2dinstantiatetriggercollider2dshooting
Script to enable Firing
I want to create a script so that when the player touches an object, the player can now shoot. But with my script, the player can no longer shoot when he is no longer in contact with the object. I want the player to shoot even after he is no longer in contact with the object. Can anyone help?
void OnTriggerEnter2D(Collider2D other){ if (other.tag == "Blue cloud") { if (Input.GetButton ("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; Instantiate (shot, ShotSpawn.position, ShotSpawn.rotation);}}}
Comment
Best Answer
Answer by Namey5 · Sep 11, 2016 at 06:54 AM
boolean canShoot = false;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Blue cloud")
canShoot = true;
}
void Update ()
{
if (canShoot) {
if (Input.GetButton ("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
Instantiate (shot, ShotSpawn.position, ShotSpawn.rotation);
}
}
}