using UnityEngine; public class HorizontalLaser : MonoBehaviour { public GameObject horizontalLaserPrefab; public bool canFireLeft = true; public bool canFireRight = 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 (canFireLeft) { Vector3 leftSide = new Vector3( bounds.min.x - (bounds.size.x / 2.0f), bounds.center.y, bounds.center.z ); var left = Instantiate(horizontalLaserPrefab, leftSide, Quaternion.identity); var leftLaserScript = left.GetComponent(); leftLaserScript.canFireRight = false; Physics2D.IgnoreCollision( left.GetComponent(), GetComponent(), true ); } if (canFireRight) { Vector3 rightSide = new Vector3( bounds.max.x + (bounds.size.x / 2.0f), bounds.center.y, bounds.center.z ); var right = Instantiate(horizontalLaserPrefab, rightSide, Quaternion.identity); var rightLaserScript = right.GetComponent(); rightLaserScript.canFireLeft = false; Physics2D.IgnoreCollision( right.GetComponent(), GetComponent(), true ); } canFire = false; } } }