O uso principal de gatilhos:
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");
}
}
Primeiro abrimos a unidade
O plug-in easytouch é usado aqui para controlar o movimento dos personagens, e outros métodos também podem ser usados
Primeiro defina a parede (parede) e a etiqueta da caixa
Tanto a caixa quanto a parede precisam selecionar o Trigger, se não houver caixa Collider, você precisa adicioná-lo manualmente
Observação: o colisor aqui está disponível como um cuboide em forma de caixa. Se o gatilho estiver em outra forma, ele precisará ser modificado.
Os personagens devem ter Rigidbody e Collider ao mesmo tempo
Monte o seguinte código no personagem e use debug para testar se ele pode ser acionado;
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 pelo debug, pode rodar normalmente
O código neste ponto pode ser usado para muitas finalidades.
Interface de exibição do objeto de gatilho:
Se eu quiser exibir esta interface quando o personagem acionar a caixa, posso escrever isto:
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界面
}
}
}
Terminar
Se você tiver alguma dúvida, por favor deixe uma mensagem abaixo, obrigado! !
Não é fácil de fazer, peça um QwQ de três links de uma tecla ~~