Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Alternative to OnTriggerStay - to check if an object is inside a collider

Discussion in 'Scripting' started by KrisM, Mar 14, 2015.

  1. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    I'm running into a problem with colliders. I have several large windzones with colliders attached to them. I want to have wind affect a much larger number of 'smell' areas (also with colliders).

    My problem is when a new smell field comes into play, it's often instantiated inside a windzone collider. Making use of 'OnTriggerStay' causes the game to lag, and I don't really need to process the 'stay' feature continually; I'm only trying to collect a couple of details from the windzone (windspeed and direction), and only really need to do that once.

    Using OnTriggerEnter isn't useful because the 'smell' doesn't enter the collider, it's instantiated inside it.

    I could probably make use of a timer within the OnTriggerStay so it only runs the script every 15 seconds, but I'm hoping there's a better/cleaner alternative available.
     
  2. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    can't believe that OnTriggerEnter behaviour.

    did you try a rigidbody WakeUp() call after instantiation to get a OnTriggerEnter Event

    Code (CSharp):
    1. this.GetComponent<Rigidbody>().WakeUp();
     
    KrisM likes this.
  3. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    You could access the collider's (or meshfilter's) bounds and use .Contains to check if any Vector3 position is inside of it.
     
    tobad likes this.
  4. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
  5. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    Three options! Thanks for the help. I'll give them a go and see which one works with my current system.

    Great to see people are so helpful here.
     
  6. KrisM

    KrisM

    Joined:
    Oct 18, 2014
    Posts:
    28
    Sorry for the delay in responding. There were a range of secondary things I thought I needed to do before I could get some of these solutions working.

    The simple 'WakeUp' function turned out to be the easiest fix.. and this made it easier to create a list of game objects affecting each 'smell field', which could be accessed via Update. I'm still learning how the Unity system functions, and some of the basics are still unknown to me; which is why this 'WakeUp' seems so valuable to a variety of areas in my game.

    Thanks for the help (all of you).
     
  7. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    good to know that it works with this single line!

    i also had problems with not detecting collisions / trigger because of the WakeUp function - this was purly desperation!

    as far as i remember was it a kinematic rigidbody switched to isKinematic = false to react with the world. but without the rigidbody.WakeUp() the physics didnt fire any trigger and collisions events.

    good to hear that this awful experience can now be used as benefit ;)