using UnityEngine; public class VerticalLaser : MonoBehaviour { public GameObject verticalLaserPrefab; public bool canFireTop = true; public bool canFireBottom = true; private Bounds bounds; private bool canFire = true; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { SpriteRenderer spriteRenderer = GetComponent(); bounds = spriteRenderer.bounds; var walls = GameObject.FindGameObjectsWithTag("Wall"); if (walls.Length > 0) { // Cap the firing ability if the bar is already intersecting with a wall. foreach (var wall in walls) { if (wall.GetComponent().bounds.Intersects(bounds)) { canFire = false; break; } } } } // Update is called once per frame void FixedUpdate() { if (canFire) { if (canFireTop) { Vector3 topSide = new Vector3( bounds.center.x, bounds.max.y + (bounds.size.y / 2.0f), bounds.center.z ); var top = Instantiate(verticalLaserPrefab, topSide, Quaternion.identity); var topLaserScript = top.GetComponent(); topLaserScript.canFireBottom = false; Physics2D.IgnoreCollision( top.GetComponent(), GetComponent(), true ); } if (canFireBottom) { Vector3 bottomSide = new Vector3( bounds.center.x, bounds.min.y - (bounds.size.y / 2.0f), bounds.center.z ); var bottom = Instantiate(verticalLaserPrefab, bottomSide, Quaternion.identity); var bottomLaserScript = bottom.GetComponent(); bottomLaserScript.canFireTop = false; Physics2D.IgnoreCollision( bottom.GetComponent(), GetComponent(), true ); } canFire = false; } } }