Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Created July 17, 2017 16:37
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/e51b1e13294ffdd1e4f612e3117475ad to your computer and use it in GitHub Desktop.
Save JonathanYin/e51b1e13294ffdd1e4f612e3117475ad to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
public class Shield : MonoBehaviour
{
public GameObject player1;
public GameObject player2;
public GameObject shield;
private float wait = 0f;
public GameObject particle1;
public GameObject particle2;
/*void Update()
{
StartCoroutine("Example");
}*/
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Player1")
{
//particle1.SetActive(true); //sets crystal above player to true
GUIComponent.text = "Immune";
player1.tag = "Immune"; //player tag becomes immune
//RobotController.maxSpeed = 18; //increases player speed
shield.SetActive(false); //deactivate immune powerup (NEXT LINE OF CODE WILL NOT WORK IF OBJECT IS DESTROYED)
//yield return new WaitForSeconds(3); //wait for 3 seconds
Invoke("ResetInvulnerability1", 10.0f);
//wait = Time.time + 5f;
//player1.tag = "Player1";
particle1.SetActive(false); //sets crystal above player to false
}
else if (col.tag == "Player2")
{
GUIComponent1.text = "Immune";
player2.tag = "Immune";
//RobotController1.maxSpeed = 18;
//particle2.SetActive(true);
//shield.transform.SetParent(player2.transform);
shield.SetActive(false);
//yield return new WaitForSeconds(3);
Invoke("ResetInvulnerability2", 10.0f);
//wait = Time.time + 5f;
//player2.tag = "Player2";
particle2.SetActive(false);
}
}
void ResetInvulnerability1()
{
Debug.Log("immunity is over");
/*for (int i = 18; i > 8; i--)
{
RobotController.maxSpeed = i;
}*/
GUIComponent.text = "P1";
player1.tag = "Player1";
}
void ResetInvulnerability2()
{
Debug.Log("immunity is over");
/*for (int i = 18; i > 8; i--)
{
RobotController1.maxSpeed = i;
}*/
GUIComponent1.text = "P2";
player2.tag = "Player2";
}
}
@JonathanYin
Copy link
Author

This script, which is attached to a "Shield" object, is another in-game pick-up. Since all the obstacles and enemies in my game destroy the players by detecting their tag as either "Player1" or "Player2", this power-up changes the player's tag, resulting in them becoming immune to these obstacles. In addition, their GUIComponent text is set to the word "Immune", letting the player know that they are currently immune. After ten seconds, the immunity passes, and their tag and GUIComponent text tag is reset.

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