unity碰撞的监测和监听
1.创建一个地面
2.去资源商店下载一个火焰素材
3.把procedural fire导入到自己的项目包管理器中
4.给magic fire 0 挂在碰撞组件Rigidbody , Sphere Collider
5.创建脚本test 并挂在magic fire 0
脚本代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
// 创建 一个爆炸的预设体
public GameObject Prefab;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
// 监听发生碰撞
private void OnCollisionEnter(Collision collision)
{
// 创建一个爆炸物体
Instantiate(Prefab, transform.position, Quaternion.identity);
// 销毁自身
Destroy(gameObject);
}
// 持续碰撞中
private void OnCollisionStay(Collision collision)
{
}
// 结束碰撞
private void OnCollisionExit(Collision collision)
{
}
}
6.把explosion 加到test 脚本的预制件
7.创建碰撞销毁脚本BZ,并挂在到EXPLOSION
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bz : MonoBehaviour
{
float timer = 0;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if (timer > 1)
{
Destroy(gameObject);
}
}
}
运行结果
untiy 3D碰撞监测