using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collider_Class : MonoBehaviour {
// 用于Collider类的测试
public GameObject OneBullet;
Transform Father;
void Start ()
{
Father = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.F))
{
GameObject NewBullet = GameObject.Instantiate<GameObject>(OneBullet, Father);
NewBullet.transform.GetComponent<Rigidbody>().AddForce(NewBullet.transform.forward * 2000,ForceMode.Force);//增加力
NewBullet.name = "Monster";
Destroy(NewBullet,5f);//摧毁
}
}
#region// 触发器的三个事件
// 进入触发范围会调用一次
void OnTriggerEnter(Collider other)
{
Debug.Log("有人进入触发区,名字是:"+ other.transform.gameObject.name);
}
// 当持续在触发范围内发生时调用
void OnTriggerStay(Collider other)
{
Debug.Log("怪兽持续在触发区,名字是" + other.transform.gameObject.name);
}
// 离开触发范围会调用一次
void OnTriggerExit(Collider other)
{
Debug.Log("怪兽离开触发区,名字是" + other.transform.gameObject.name);
}
#endregion
#region //碰撞相关的三个方法
// 碰撞开始会调用一次
void OnCollisionEnter(Collision other)
{
Debug.Log("进入碰撞区,碰撞到的物体叫:"+ other.transform.gameObject);
other.transform.GetComponent<Rigidbody>().AddTorque(new Vector3(0,10,0),ForceMode.Impulse);//让他旋转起来
}
// 当碰撞持续发生时调用
void OnCollisionStay(Collision other)
{
//检测和谁发生碰撞
if (string.Equals("Monster", other.gameObject.name))//如果名字匹配
{
print("怪兽撞击我们的隔离墙");
}
Debug.Log("持续碰撞,碰撞到的物体叫:" + other.transform.gameObject);
}
// 碰撞结束会调用一次
void OnCollisionExit(Collision other)
{
Debug.Log("离开碰撞,离开碰撞的物体叫:" + other.transform.gameObject);
}
#endregion
}
Unity 触发检测与碰撞检测的示例
Leoysq2023-10-15 16:24
相关推荐
张老师带你学1 小时前
Unity 机器人 humanoid +shader效果星河耀银海7 小时前
Unity基础:UI组件详解:Toggle开关的状态控制mxwin7 小时前
Unity URP 法线贴图:世界空间 vs 切线空间 深度解析两种法线贴图格式在实时渲染中的核心差异、适用场景与性能权衡℡枫叶℡1 天前
Unity - 全局配置Unity工程的资源检索的目录mxwin1 天前
Unity URP 下 TBN 矩阵学习 切线空间、tangent.w 与镜像 UV 的那些坑程序猿多布1 天前
Unity导表工具解决方案-Luban使用教程mxwin1 天前
Unity URP Shader 混合模式完全指南mxwin1 天前
Unity URP 下 HDR 与 Tonemapping 的 Shader 意识沉默金鱼1 天前
U3D高级编程:主程手记——第二章2.1读书笔记mxwin2 天前
Unity Shader 深度写入与关闭ZWrite Off · 半透明排序 · 粒子穿插