Advertisement
LeeMace

Destroy Out Of Bounds

Nov 28th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DestroyOutOfBounds : MonoBehaviour
  6. {
  7.     //script to destroy gameobject afte they leave the scene
  8.     private float topBound = 30.0f;
  9.     private float lowerBound = -50.0f;
  10.     private float sideBound = 30f;
  11.     private GameManager gameManager;
  12.  
  13.     void Start()
  14.     {
  15.         //refernecing the game manager
  16.         gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
  17.     }
  18.     //when food or animal reach a certain top or bottom positon they are destroyed
  19.     //takes away a life when animal reaches boundary
  20.     void Update()
  21.     {
  22.         if (transform.position.z > topBound)
  23.         {
  24.             Destroy(gameObject);
  25.         }
  26.         else if (transform.position.z < lowerBound)
  27.         {
  28.             gameManager.AddLives(-1);
  29.             Destroy(gameObject);
  30.         }
  31.         else if (transform.position.x > sideBound)
  32.         {
  33.             gameManager.AddLives(-1);
  34.             Destroy(gameObject);
  35.         }
  36.         else if (transform.position.x < -sideBound)
  37.         {
  38.             gameManager.AddLives(-1);
  39.             Destroy(gameObject);
  40.  
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement