TO SQUASH: Added shooting bars.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class HorizontalBar : MonoBehaviour
|
||||
{
|
||||
public float firingSpeed = 0.03f;
|
||||
|
||||
public GameObject horizontalBarrierPrefab;
|
||||
|
||||
public bool canFireLeft = true;
|
||||
|
||||
public bool canFireRight = true;
|
||||
|
||||
private float firingTimer = 0.0f;
|
||||
|
||||
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<SpriteRenderer>();
|
||||
bounds = spriteRenderer.bounds;
|
||||
|
||||
var walls = GameObject.FindGameObjectsWithTag("Wall");
|
||||
|
||||
if (walls.Length > 0)
|
||||
{
|
||||
foreach (var wall in walls)
|
||||
{
|
||||
if (wall.GetComponent<Collider2D>().bounds.Intersects(bounds))
|
||||
{
|
||||
canFire = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
if (canFire)
|
||||
{
|
||||
firingTimer += Time.fixedDeltaTime;
|
||||
|
||||
if (firingTimer >= firingSpeed)
|
||||
{
|
||||
if (canFireLeft)
|
||||
{
|
||||
Vector3 leftSide = new Vector3(
|
||||
bounds.min.x - (bounds.size.x / 2.0f),
|
||||
bounds.center.y,
|
||||
bounds.center.z
|
||||
);
|
||||
|
||||
var left = Instantiate(horizontalBarrierPrefab, leftSide, Quaternion.identity);
|
||||
var leftBarScript = left.GetComponent<HorizontalBar>();
|
||||
leftBarScript.firingSpeed = firingSpeed;
|
||||
leftBarScript.canFireRight = false;
|
||||
|
||||
Physics2D.IgnoreCollision(
|
||||
left.GetComponent<Collider2D>(),
|
||||
GetComponent<Collider2D>(),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
if (canFireRight)
|
||||
{
|
||||
Vector3 rightSide = new Vector3(
|
||||
bounds.max.x + (bounds.size.x / 2.0f),
|
||||
bounds.center.y,
|
||||
bounds.center.z
|
||||
);
|
||||
var right = Instantiate(horizontalBarrierPrefab, rightSide, Quaternion.identity);
|
||||
var rightBarScript = right.GetComponent<HorizontalBar>();
|
||||
rightBarScript.firingSpeed = firingSpeed;
|
||||
rightBarScript.canFireLeft = false;
|
||||
|
||||
Physics2D.IgnoreCollision(
|
||||
right.GetComponent<Collider2D>(),
|
||||
GetComponent<Collider2D>(),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
canFire = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user