Question by
$$anonymous$$ · Sep 09, 2015 at 06:58 PM ·
c#2d gametrigger
Obstacle appear when the player enter in a Trigger
I am trying to do a 2D game. What i dont know is this: I need to detect if the player entered on a trigger, next i need to a obstacle appear and when the player enter in other trigger, the obstacle dissappear.
Comment
Answer by Positive7 · Sep 10, 2015 at 11:18 AM
Tag your Player as "Player"
ObstacleAppear.cs :
using UnityEngine;
public class ObstacleAppear : MonoBehaviour {
public GameObject obstacleObject;
void OnTriggerEnter2D(Collider2D collider){
if (collider.gameObject.tag == "Player") {
obstacleObject.SetActive (true);
}
}
}
ObstacleDisappear .cs :
using UnityEngine;
public class ObstacleDisappear : MonoBehaviour {
public GameObject obstacleObject;
void OnTriggerEnter2D(Collider2D collider){
if (collider.gameObject.tag == "Player") {
obstacleObject.SetActive (false);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612033058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
On trigger enter void is calling twice 0 Answers
KeyCode not detected when OnTriggerStay = true 1 Answer
Detect when edge of the map is in camera view and dont random spawn enemy inside of it 0 Answers
How to assign a value to a Text Variable during runtime? 0 Answers
How to make -Count down Timer on entrance and exit triggers and display remaining time 0 Answers