Search Unity

check if gameObject is still inside trigger

Discussion in 'Scripting' started by raycosantana, Jan 12, 2015.

  1. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    This is a very basic and stupid question but I have this problem where I have a series of Spawn points where the players can spawn, I dont want two players to spawn on the same SpawnPoint so I made a little script with a Bool called IsOcupaid so I know if the Spawn point is occupied or not using a trigger, the problem here is if a player dies inside the trigger collider since the player didnt exit the trigger the Spawn point is still occupied so other players cant spawn there...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpawnPoint : MonoBehaviour {
    5.     public bool IsOcupaid = false;
    6.     // Use this for initialization
    7.     void OnTriggerExit(Collider Other){
    8.         TankController Temp = Other.GetComponent<TankController>();
    9.         if (Temp != null){
    10.             IsOcupaid = false;
    11.         }
    12.     }
    13.     void OnTriggerStay(Collider Other){
    14.         TankController Temp = Other.GetComponent<TankController>();
    15.         if (Temp != null){
    16.             IsOcupaid = true;
    17.         }
    18.    
    19.     }
    20. }
    Any ideas how can I fix this problem?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    by "dies" i assume you are using the Destroy function? one way is to add an "OnDestroy()" function to the player and have them move "up" 10000 (well outside the game area and out of view) before being destroyed, it'll all happen within 1 frame so it shouldn't be visible. The move will make the triggers react before the object ceases to exist.

    might not work if you have a 3d environment though...
     
  3. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    I dont destroy the players just disable them, but anyways I tested it using my "dead" function and moving the player just before disabling the player character, it doesnt work, it seem triggers are not fast enough and also since the camera follows the player this sudden movement is really noticeable.
     
  4. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Ok nevermind Im F***ing stupid I didnt knew I could just use an "else" inside the OnTriggerStay and it would work, I thought you would have to use OnTriggerExit to check if the object its not there, but it seems the OnTriggerExit its not needed at all.
     
    Mlisager likes this.
  5. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    I'm glad you've found a solution! In addition, I would make the IsOccupied bool a property, rather than a field, so it can't be edited by outside code:

    Code (CSharp):
    1.     bool _isOccupied = false;
    2.     public bool IsOccupied {
    3.         get {
    4.             return _isOccupied;
    5.         }
    6.         private set {
    7.             _isOccupied = value;
    8.         }
    9.     }
     
    raycosantana likes this.
  6. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    245
    You could also stick an update in there to check player status as well!

    I just made IsOcupaid hold the current player in the spawn, so the update can check if it becomes dead, and free the spawn.

    Code (CSharp):
    1.  
    2. public TankController IsOcupaid = null;
    3.    
    4. void OnTriggerEnterCollider Other){
    5.     IsOcupaid = Other.GetComponent<TankController>();
    6. }
    7. void OnTriggerExitCollider Other){
    8.     if(IsOcupaid  == Other.GetComponent<TankController>()){
    9.         IsOcupaid = null;
    10.     }
    11. }
    12. void Update(){
    13.     if(IsOcupaid != null && IsOcupaid.isDead){
    14.         IsOcupaid = null
    15.     }
    16. }
    17.  
    Also, GetComponent is a heavy function, so it's best not to put it in a function like OnTriggerStay that will be called very frequently.

    Also also, why did you spell it "IsOcupaid"? :)