Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Created July 17, 2017 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanYin/db3d2748d6bc9f36f81385f458d7ca44 to your computer and use it in GitHub Desktop.
Save JonathanYin/db3d2748d6bc9f36f81385f458d7ca44 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Destroyer : MonoBehaviour
{
public GameObject player;
public GameObject player2;
void OnTriggerEnter2D(Collider2D other)
{
//if the object that triggered the event is tagged player
if (other.tag == "Player1" && player2 == null)
{
SceneManager.LoadScene(2);
}
else if (other.tag == "Player2" && player == null)
{
SceneManager.LoadScene(2);
}
if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else
{
Destroy(other.gameObject);
}
}
}
@JonathanYin
Copy link
Author

Since my game is an infinite-runner, and there is a camera moving along at a set pace, I had to make two rectangular colliders which used this script to destroy any objects they ran into. This would destroy game objects if they left the screen area, so that there wouldn't be too many objects cluttering the game memory, and making so that the players couldn't fall behind and expect to live. In addition, if a player fell onto a destroyer, and the other player was also dead, the game would go to the "GameOver" screen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment