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
相关推荐
一线灵44 分钟前
跨平台游戏引擎 Axmol-2.9.1 发布地狱为王4 小时前
Unity使用RVM实现实时人物视频抠像(无绿幕)HahaGiver6667 小时前
Unity与Android原生交互开发入门篇 - 打开Android的设置野奔在山外的猫16 小时前
【解决】解决方案内存在对应命名空间,但程序引用显示无该命名空间问题B0URNE16 小时前
【Unity基础详解】(5)Unity核心:Coroutines协程野奔在山外的猫19 小时前
【案例】程序化脚本生成xiaotao13121 小时前
unity hub在ubuntu 22.0.4上启动卡住小句1 天前
通过图表和详细流程解释XXL-JOB中任务从创建到执行的完整过程!chen2 天前
Unity颜色曲线ColorCurvesB0URNE2 天前
【Unity基础详解】(4)Unity核心类:MonoBehaviour