Question by
Frajzee · Feb 03, 2020 at 10:48 AM ·
2d2d-platformernewbierange
Enemy Range 2D
Hi im having problem with giving my turrets range script. i want them to start shooting when my camera hits them and stop when i walk away. i tried using box collider and range bool but its not working with multiple turrets. Can anybody help me please ? Im newbie so as easy as possible please
Comment
Answer by Pacaworks · Feb 03, 2020 at 12:33 PM
@Frajzee If by "when my camera hits them" you mean when they're visible in your screen, you could use the OnBecameVisible() method. There's 2 ways that you could use: deactivate your game object or cancel the shoothing mechanic while invisible. I strongly recommend you to cancel the shooting mechanic by just using a bool variable.
Simply write this inside your turrets C# script:
private bool canIShoot = false;
public void Update
{
// Your shooting code will depend on the canIShoot variable
if (canIShoot)
{
// HERE: your shooting code
}
}
public void OnBecameVisible()
{
// If you want your turret to literally be inactive
yourTurretObject.SetActive(true);
// If you want to disable the shooting interaction
canIShoot = true;
}
public void OnBecameInvisible()
{
// If you want your turret to literally be inactive
yourTurretObject.SetActive(false);
// If you want to disable the shooting interaction
canIShoot = false;
}