- Home /
Player Tips - Have triggers act independently
I have these GameObjects that when the player enters they glow up and allow the player to interact. The problem is, that if I have multiple Objects (Tips) and you enter one of their triggers all of the other ones also glow. I attached a GIF to show what I mean. If you have any suggestions for code please let me see! My code is a little confusing: Basically I have two Tip Objects. One for when the player is in the trigger and one for when they aren't (glowing and not glowing) All the TipObjects in the scene are added to an ObjectArray, and the glowing objects are teleported away as they don't work if I set them to not active.
On collision with player, the non glowing is set not active and the glowing is put in the right position and the tip displays.
I have commented my code in the hope it makes sense.
But back to my main question...why do all the triggers act like one trigger and cause all the objects to glow upon collision with player?
Code:
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
Tip1 = GameObject.FindGameObjectsWithTag("Tip1");
Tip2 = GameObject.FindGameObjectsWithTag("Tip2");
foreach (GameObject Tip in Tip1)
{
Tip.SetActive(true);
}
foreach (GameObject Tip2 in Tip2)
{
Tip2.SetActive(true);
Tip2.transform.localPosition = new Vector3(1000, 1000); //Teleport glowing object away
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Return) && InTrigger == true)
{
TipShowing = !TipShowing; //TipShowing being the actual tip after the intro Text (Press Enter)
if (TipShowing == true) //If "Press Enter" is showing...
{
playerScript.CanMove = false; //player can't move
//Debug.Log("Enter");
TipObject.SetActive(true); //Object containing the text is shown
IntroCanvas.SetActive(false); //"Press Enter" is hidden
}
if (TipShowing == false) //Player presses enter again hence closing tip
{
//Debug.Log("Enter2");
playerScript.CanMove = true;
TipObject.SetActive(false);
IntroCanvas.SetActive(true);
TipShowing = false;
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
foreach (GameObject Tip in Tip1) //On collision with player all the non glowing objects are hidden
{
Tip.SetActive(false);
}
foreach (GameObject Tip2 in Tip2) //All the glowing objects are shown at their local origin
{
Tip2.SetActive(true);
Tip2.transform.localPosition = new Vector3(0, 0);
}
IntroCanvas.SetActive(true); //Canvas to tell player to press enter is shown
InTrigger = true; //In trigger set to true
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
foreach (GameObject Tip in Tip1)
{
Tip.SetActive(true);
}
foreach (GameObject Tip in Tip2)
{
Tip.SetActive(false);
}
IntroCanvas.SetActive(false);
InTrigger = false;
}
}
Gif: Stored as a Gyazo
Answer by Chik3r · Dec 28, 2018 at 05:05 PM
When the player enters the trigger, you making all tips glow. This is your code, but only making the GameObject where the player is glow. Code:
public GameObject thisTip;
public GameObject thisTipGlow;
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
Tip1 = GameObject.FindGameObjectsWithTag("Tip1");
Tip2 = GameObject.FindGameObjectsWithTag("Tip2");
foreach (GameObject tip in Tip1)
{
tip.SetActive(true);
}
foreach (GameObject tip in Tip2)
{
Tip2.SetActive(true);
Tip2.transform.localPosition = new Vector3(1000, 1000); //Teleport glowing object away
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Return) && InTrigger == true)
{
TipShowing = !TipShowing; //TipShowing being the actual tip after the intro Text (Press Enter)
if (TipShowing == true) //If "Press Enter" is showing...
{
playerScript.CanMove = false; //player can't move
//Debug.Log("Enter");
TipObject.SetActive(true); //Object containing the text is shown
IntroCanvas.SetActive(false); //"Press Enter" is hidden
}
else //Player presses enter again hence closing tip
{
//Debug.Log("Enter2");
playerScript.CanMove = true;
TipObject.SetActive(false);
IntroCanvas.SetActive(true);
TipShowing = false;
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
thisTip.SetActive(false);
thisTipGlow.SetActive(true);
IntroCanvas.SetActive(true); //Canvas to tell player to press enter is shown
InTrigger = true; //In trigger set to true
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
thisTip.SetActive(true);
thisTipGlow.SetActive(false);
IntroCanvas.SetActive(false);
InTrigger = false;
}
}
And you only have to change thisTip/thisTipGlow to the actual tip. Additionally, If this script is in every tip, I would change your code to be something like this:
public GameObject thisTip;
public GameObject thisTipGlow;
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
Tip1 = GameObject.FindGameObjectsWithTag("Tip1");
Tip2 = GameObject.FindGameObjectsWithTag("Tip2");
thisTip = this.GameObject;
thisTip.SetActive(true);
thisTipGlow.SetActive(false);
thisTipGlow.transform.localPosition = new Vector3(1000, 1000); //Teleport glowing object away
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Return) && InTrigger == true)
{
TipShowing = !TipShowing; //TipShowing being the actual tip after the intro Text (Press Enter)
if (TipShowing == true) //If "Press Enter" is showing...
{
playerScript.CanMove = false; //player can't move
//Debug.Log("Enter");
TipObject.SetActive(true); //Object containing the text is shown
IntroCanvas.SetActive(false); //"Press Enter" is hidden
}
else //Player presses enter again hence closing tip
{
//Debug.Log("Enter2");
playerScript.CanMove = true;
TipObject.SetActive(false);
IntroCanvas.SetActive(true);
TipShowing = false;
}
}
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Player")
{
thisTip.SetActive(false);
thisTipGlow.SetActive(true);
IntroCanvas.SetActive(true); //Canvas to tell player to press enter is shown
InTrigger = true; //In trigger set to true
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
thisTip.SetActive(true);
thisTipGlow.SetActive(false);
IntroCanvas.SetActive(false);
InTrigger = false;
}
}
And then you set thisTip
and thisTipGlow
in the inspector to what they should be.
This worked perfectly! The only thing is the lines: thisTip = this.GameObject; ... thisTipGlow.transform.localPosition = new Vector3(1000, 1000);
Are now unnecessary due to how I have my tips setup (tipnot glow and tipglow are setup as children of one object and doing this.gameobject
got the parent object ins$$anonymous$$d which just disabled the whole thing.
Thanks for your help :D
Your answer
Follow this Question
Related Questions
Trying to get Player Tip for player to open on Return and close on Return 2 Answers
How do i make a one gun only Weapon system. (2D) 2 Answers
In my 2D platformer game, how would I create a height marker?, 1 Answer
How to make character stop at wall? 1 Answer
How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer