Showing a UI canvas when player enters a specific location
Hello, I am making a game like forza horizon, I am new so making this project to learn but I wanna test somethings out and I want this in my game after it's finished, but I only know a code to show a button UI that changes scene, but I need to know how can I disable that scene and show it when a player enters a specific location or object
Example:
When you enter the front of the garage you get this to see "Enter Garage" and when your to far away it wil disappear!
Answer by $$anonymous$$ · Mar 04, 2017 at 11:32 AM
What i suggest is you make a big cube (object) in the area where the player will trigger the UI, with the mesh renderer off and on the cube colider select is trigger...
Then all you have to do is attach this script to the Cube
// C# code
public GameObject UI;
void OnTriggerEnter(Collider Obj)
{
if(Obj.tag == "Player")
{
UI.SetActive(true);
}
}
void OnTriggerExit(Collider Obj)
{
if (Obj.tag == "Player")
{
UI.SetActive(false);
}
}
// Give the player obj the Player tag
Answer by hexagonius · Mar 04, 2017 at 11:27 AM
Place a trigger box or sphere infront of the garage. Add a script with OnTriggerEnter and OnTriggerExit methods in it. WIthin these methods switch your button on and off