MIF_E31221325/Assets/Sprites/Space Shooter Template FREE/Scripts/Boundary.cs

40 lines
1.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// This script defines the size of the Boundary depending on Viewport. When objects go beyond the Boundary, they are destroyed or deactivated.
/// </summary>
public class Boundary : MonoBehaviour {
BoxCollider2D boundareCollider;
//receiving collider's component and changing boundary borders
private void Start()
{
boundareCollider = GetComponent<BoxCollider2D>();
ResizeCollider();
}
//changing the collider's size up to Viewport's size multiply 1.5
void ResizeCollider()
{
Vector2 viewportSize = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)) * 2;
viewportSize.x *= 1.5f;
viewportSize.y *= 1.5f;
boundareCollider.size = viewportSize;
}
//when another object leaves collider
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.tag == "Projectile")
{
Destroy(collision.gameObject);
}
else if (collision.tag == "Bonus")
Destroy(collision.gameObject);
}
}