Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

OnTriggerStay al reves?

Iniciado por Abramelin, Abril 30, 2016, 04:20:11 PM

Tema anterior - Siguiente tema
Tengo un trigger en el que quiero chequear si X objetos no están. ¿como se podria usar esta funcion al reves? con OnTriggerExit creo que no me sirve para lo que quiero hacer.Esto, pero correctamente. 
function OnTriggerNotStay (other : Collider){
if (other.gameObject.tag == "bichos"){

//loquesea

}
}
 

Opción a)  y si haces un dowhile loop? Opción y si,  sencillamente,  le das la instrucción contraria la que tienes en la cabeza?  


Abril 30, 2016, 06:42:14 PM #3 Ultima modificación: Abril 30, 2016, 06:47:47 PM por Abramelin
a ver me explico mejor, lo que quiero formular es "si dentro del trigger no hay ningun objeto con el tag "bichos"".....(imaginemos que dentro del trigger hay 10 bichos, en el momento que no quede ninguno se activaria una cinematica por ej)OnTriggerNotStay no existe, ya lo se, lo he puesto para que se entienda lo que quiero hacer. el dowhile loop no se lo que es ahora lo miro,... la opcion contraria sería ponerle un else, te refieres a eso? si es asi, cuando esté dentro del trigger cualquier otro objeto con otra tag se activaria igualmente, al menos como lo acabo de probar.  

Vos mismo te estas respondiendo, con OnTriggerStay verifica cada "colision" y su tag, si no existe esa tag es que no hay tal objeto.

podrias crear un script con una array de booleanas y ponerlas true/false cuando X objeto entre/salga
var outsideObjects : GameObject[]; //Objetos a detectar
var outsideBool : bool[]; //Indica si el objecto "i" está dentro o fuera (false = fuera; true = dentro)

function Awake () {
    outsideBool = new bool[outsideObjects.Lenth];
}

function OnTriggerEnter (other : Collider) {
    for (int i = 0; i < outsideObjects.Length; i++) {
        if (other.gameObject == outsideObjects) {
            outsideBool = true;
        }
    }
}
function OnTriggerExit (other : Collider) {
    for (int i = 0; i < outsideObjects.Length; i++) {
        if (other.gameObject == outsideObjects) {
            outsideBool = false;
        }
    }
}

function Update () {
    for (int i = 0; i < outsideObjects.Length; i++) {
        if (outsideBool == true) {
            //Si el objeto "i" (outsideObjects) está dentro
        }
        else {
            //Si el objeto "i" (outsideObjects) está fuera
        }
    }
}
 Tal vez no sea el metodo mas eficiente pero es lo primero que se me ha ocurridoPD: disculpad si he cometido algun fallo en el script, hace mucho que no porgramo en JS

Yo creo que la lógica de "si no están" no es la mejor para realizar el checkeo. Hay multitud de cosas que pueden hacer que ese checkeo diera true...¿No sería mejor replantearte la condición de lo que debe entrar?

Abril 30, 2016, 08:53:22 PM #7 Ultima modificación: Abril 30, 2016, 08:57:44 PM por Abramelin
Bueno como ultimo recurso tiraría del mismo (y tedioso) metodo de siempre, que sería crear un objeto invisible por cada enemigo destruido, y cuando llegase a 10 por ejemplo, ejecutar ya lo que sea... pero es sobre todo por ahorrar trabajo.Siguiendo con el OnTriggerStay, he probado estas dos formas:con esta no hace nada:
var luces : GameObject;
var objetoact : GameObject;
var objetdesact : GameObject;


function OnTriggerStay (other:Collider){   
if (other.gameObject.tag == "bichos"){

luces.SetActive(true);

}

else{
if (other.gameObject.tag == "bichos"){
objetoact.SetActive(true);
objetdesact.SetActive(false);
luces.SetActive(false);
gameObject.SetActive(false);
}
}
}
y con esta se activa todo en el momento que el player entra en el trigger:
var luces : GameObject;
var objetoact : GameObject;
var objetdesact : GameObject;


function OnTriggerStay (other:Collider){   
if (other.gameObject.tag == "bichos"){

luces.SetActive(true);

}

else{
objetoact.SetActive(true);
objetdesact.SetActive(false);
luces.SetActive(false);
gameObject.SetActive(false);
}
}

Sería muy comodo poder chequear si un objeto no se encuentra dentro del trigger directamente con una funcion.

Si lo que quieres es que cuando no se detecte la presencia de X numero de objetos se active una animación, prueba mediante una variable int llevar el número de objetos que hay y vas reduciendo el número a medida de que OnTriggerExit() deje de detectar un collider, finalmente cuando la variable llegue a 0 realizas la cinematica o cualquier otra cosa. 

Abril 30, 2016, 11:32:40 PM #9 Ultima modificación: Abril 30, 2016, 11:50:26 PM por Abramelin
Creo que esa es la forma mas sencilla. Algo asi creo que irá bien.
var luces : GameObject;
var objetoact : GameObject;
var objetdesact : GameObject;

var bichor : int = 10;


function OnTriggerExit (other:Collider){   
if (other.gameObject.tag == "bichos"){

bichor -= 1;   
}
}

function Update (){
if(bichor==0){
objetoact.SetActive(true);
objetdesact.SetActive(false);
luces.SetActive(false);
gameObject.SetActive(false);
}
}

*EDITO: como OnTriggerExit no reconoce los objetos al destruirse y la variable no baja.

Mayo 01, 2016, 02:05:03 PM #10 Ultima modificación: Mayo 01, 2016, 02:06:06 PM por Jose134
si quieres saber cuantos bichos hay en la escena puedes hacer esto
var bichosArray : GameObject[];

function Update () {
    bichosArray = GameObject.FindGameObjectsWithTag("bichos");
}
Y ahora podrias hacer algo asi
private var bichosArray : GameObject[]; //Bichos que hay en la escena
private var bichosLeft : int; //Bichos que han salido del trigger

public var bichos : int;

function OnTriggerExit (other : Collider) {
    if (other.gameObejct.tag == "bichos") {
        bichosLeft += 1; //Si un bicho sale del trigger se suma 1 a la variable
    }
}

function Update () {
    bichosArray = GameObject.FindGameObjectsWithTag("bichos"); //Busca todos los bichos que hay
    bichos = bichosArray.Length - bichosLeft; //Los bichos que quedan son el total de los que hay menos los que han salido

   
    if (bichos == 0) {
        //Activar/Desactivar los objetos que quieras
    }
}
 

Bueno, al final lo solucione sustituyendo el checkeo en exit del tag "bichos", por otro de entrada "puntos" que corresponde a las particulas que se crean al destruirse el bicho. Ya va bien. Asi a quedado: 
var luces : GameObject;
var objetoact : GameObject;
var objetdesact : GameObject;

var bichor : int = 10;


function OnTriggerEnter (other:Collider){   
if (other.gameObject.tag == "puntos"){

bichor -= 1;   
}
}

function Update (){
if(bichor==0){
objetoact.SetActive(true);
objetdesact.SetActive(false);
luces.SetActive(false);
gameObject.SetActive(false);
}
}

Etiquetas: