Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DestroyOutOfBounds : MonoBehaviour
- {
- //script to destroy gameobject afte they leave the scene
- private float topBound = 30.0f;
- private float lowerBound = -50.0f;
- private float sideBound = 30f;
- private GameManager gameManager;
- void Start()
- {
- //refernecing the game manager
- gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
- }
- //when food or animal reach a certain top or bottom positon they are destroyed
- //takes away a life when animal reaches boundary
- void Update()
- {
- if (transform.position.z > topBound)
- {
- Destroy(gameObject);
- }
- else if (transform.position.z < lowerBound)
- {
- gameManager.AddLives(-1);
- Destroy(gameObject);
- }
- else if (transform.position.x > sideBound)
- {
- gameManager.AddLives(-1);
- Destroy(gameObject);
- }
- else if (transform.position.x < -sideBound)
- {
- gameManager.AddLives(-1);
- Destroy(gameObject);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement