- Home /
How to make multiple gameobjects instantiate from one collider?
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.
Answer by Ark_Revan · May 19, 2017 at 02:05 PM
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.
I forgot, the collider is (probably) some kind of triangle, and it is attach to an empty game object tagged with this new tag "FieldOfVision", then finally, you attach the object to the player( or his camera ).
All of this depends on the kind of game you want to do ( FPS / TPS / str ? )
Thanks for all the help. Just wonder, should I change other.tag to "Item" since that script above is going to be attached to player? Or is that script suppose to be attached to the Items?
No need to change other.tag.
If you create this FieldOfVision, then you check that the item "Enter the field of vision Collider". In order to check for this, you have to attach the script to the FieldOfVision object.
I just realized that the tag I wrote was a bad design choice. You should probably write something like : if ( other.tag =="ObjectWithUi") or whatever name you want. Sorry that the name I choose was misleading.
Thanks , that sounds like it would work much better than my current way of trying to instantiate the UI via a script in the player. I will try it as soon as I get home. One thing though , when I add a trigger to the item's collider the item falls through the ground , it does have a rigidbody, so I'm not sure if I'm overlooking something simple or if I need to add a script.
Set the item's gravity scale to 0 in the rigidbody editor.
When a collider is set to "isTrigger", it lose his blocking ability.
What you should do is to let your first collider as it was, and create a new collider which "is trigger" and will help you create some "event" like "the player is looking".
You can put as many collider as you want to an object. But be careful to only have one which is responsible for collision. If there are more than one collider which doesn't have the IsTrigger option, then you will probably see some REALLY weird behaviour like the object suddenly flying in the air ( because he collide with himself so he's pushed ).
Answer by shadowpuppet · May 19, 2017 at 01:40 PM
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
Thanks, will try that as soon as I get home ! One thing i remember happening though is my items would fall through the ground if I added is trigger to their collider even though they do have a rigidbody.. :(
Answer by archelyte_vz · May 22, 2017 at 07:34 AM
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<Image>(); while (uiImage.alpha < 1) { uiImage.alpha += Time.deltaTime; yield return null; } }