I’m trying to figure out what the best way to instantiate multiple game objects from one large collider
I tried attaching an invisible collider (is trigger) to my player and putting a bunch of gameobjects in front of him , what I’m trying to do is if he’s looking in their direction the items would detect the player’s invisible collider and instantiate a gameobject (which is a UI) right above them.
What I would do is put a collider called “field of vision” attach to the player, and this collider would change a bool attach to each object which as an UI. If the bool is true, then the object display is UI, else , it hides it.
function OnTriggerEnter (Collider other) {
if(other.tag=="FieldOfVision") {
bool = true;
// or even directly UI.renderer.enable = true; if it works like this ( I don't use UI often^^")
}
}
And the opposite OnTriggerExit.
try it the other way around? put the trigger on the items with an
void OnTriggerEnter (Collider other) {
if(other.tag == “Player”){
script sow hen the player looks at them and his collider is in trigger zone UI element appears
Well if the objects are invisible until the player looks at them, then there are a few potential snags.
- If the objects are not already active in some way in the scene, it will very hard (if not impossible) for them to detect another object, because they are not active.
- If the objects are not already instantiated, they are likely not active either.
What may be a better solution is to have them already instantiated, active, and invisible (alpha at 0) at first. From there, when they detect the player change their alpha (1).
That would look something like this (attached to the object, not the player):
`void OnTriggerEnter (Collider trigger) {
if (trigger.gameObject.tag == “Player”) {
StartCoroutine(“DoFade”);
}
}
IEnumerator DoFade() {
Image uiImage= GetComponent();
while (uiImage.alpha < 1) {
uiImage.alpha += Time.deltaTime;
yield return null;
}
}`