Unity 学习笔记汇总
Collider官方API使用文档
1. 前台
将Cube
的Is Trigger对应的复选框打上勾。
2. 代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
Debug.Log("触发进入");
Debug.Log(other.gameObject.name);
Destroy(other.gameObject); //销毁的是球
Destroy(gameObject); //销毁的是Cube
}
private void OnTriggerStay(Collider other)
{
Debug.Log("触发停留");
}
void OnTriggerExit(Collider other)
{
Debug.Log("触发退出");
}
}
3. 结果
Cube
与Sphere
碰撞时,会打印相应的信息。
开始运行后,点击Scene,选中Sphere再按下W键,可以对Sphere进行移动。