El uso principal de los disparadores:
void OnTriggerEnter()
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag =="A") //如果它触发的物体的tag叫A
{
Debug.Log("ontriggerA");
}
else if (other.gameObject.tag == "B")//如果它触发的物体的tag叫B
{
Debug.Log("ontriggerB");
}
}
Primero abrimos la unidad
El complemento easytouch se usa aquí para controlar el movimiento de los personajes, y también se pueden usar otros métodos
Primero configure la pared (pared) y la etiqueta de la caja
Tanto el cuadro como el muro deben seleccionar Trigger, si no hay un cuadro Collider, debe agregarlo manualmente
Nota: El colisionador aquí está disponible como un paralelepípedo en forma de caja. Si el gatillo tiene otra forma, debe modificarse.
Los personajes deben tener Rigidbody y Collider al mismo tiempo
Monte el siguiente código en el personaje y use la depuración para probar si se puede activar;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "wall") //如果它触发的物体的tag叫wall
{
Debug.Log("TriggerWall");
}
else if (other.gameObject.tag == "box")//如果它触发的物体的tag叫box
{
Debug.Log("Triggerbox");
}
}
Visto por depuración, puede ejecutarse normalmente
El código en este punto se puede utilizar para muchos propósitos.
Interfaz de visualización de objetos de activación:
Si quiero mostrar esta interfaz cuando el personaje activa el cuadro, puedo escribir esto:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ontrigger : MonoBehaviour
{
public GameObject UImsg;
void Start()
{
UImsg.SetActive(false); //不显示UImsg界面
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "wall") //如果它触发的物体的tag叫wall
{
Debug.Log("TriggerWall");
}
else if (other.gameObject.tag == "box")//如果它触发的物体的tag叫box
{
Debug.Log("Triggerbox");
UImsg.SetActive(true); //显示UImsg界面
}
}
}
Finalizar
Si tiene alguna pregunta, deje un mensaje a continuación, ¡gracias! !
No es fácil de hacer, solicite un QwQ de tres enlaces de una tecla ~~