Search Unity

Object instantiation upon triggerS?

Discussion in 'Scripting' started by mikelowefedex, Nov 14, 2020.

  1. mikelowefedex

    mikelowefedex

    Joined:
    Sep 29, 2020
    Posts:
    139
    Hello,
    I have a scenario where I would like all the triggers that my main camera 'stays' in, to instantiate specific game objects to be at the location(s), rotation(s) of each trigger. My main camera can be inside 2 or more triggers at the same time, which there is a rigidbody and a sphere collider attached to the camera. I am using the OnTriggerStay() function, but that function can only focus on 1 trigger at a time. I would like to have it so the first instance of OnTriggerStay() cleans up the list of triggers in my c# code, and each unique trigger after that re-adds each trigger to the 'list'. Then, I can instantiate game objects to be at those locations and rotations. The scaling doesn't matter and can be the original sizing of the game object, and not the trigger. This will make it so I can have a building, and when I stand at a certain part of the building, only the room I am standing in, gets rendered, and if I am standing near 2 rooms, 2 rooms get rendered... and a room is the game object I'd like to instantiate per trigger. How would I do this? Thank you.
     
    Last edited: Nov 14, 2020
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    From your description, I don't think
    OnTriggerStay 
    is your best choice. Consider using
    OnTriggerEnter 
    to instantiate a room, then
    OnTriggerExit 
    to destroy it.
     
  3. mikelowefedex

    mikelowefedex

    Joined:
    Sep 29, 2020
    Posts:
    139
    Thank you. That worked.
     
    Stevens-R-Miller likes this.