unity 利用触发碰撞实现物体自发光
时间: 2023-09-16 10:11:00 浏览: 126
可以通过在物体上添加自发光材质来实现物体的自发光效果。具体的实现步骤如下:
1. 在 Unity 中创建一个新的材质,并将 Shader 设置为 Standard Shader。
2. 在 Material 中启用 Emission 属性,并设置自发光的颜色和强度。
3. 将这个材质应用到需要实现自发光的物体上。
4. 在实现触发碰撞的脚本中,使用 GetComponent 获取物体的 Renderer 组件,并将其 Material 的 EnableEmission 属性设置为 true。
5. 当碰撞发生时,将物体的 Material 的 EnableEmission 属性设置为 false。
以下是一个示例代码:
```
public class GlowOnTrigger : MonoBehaviour
{
private Renderer _renderer;
private Material _material;
private void Start()
{
_renderer = GetComponent<Renderer>();
_material = _renderer.material;
}
private void OnTriggerEnter(Collider other)
{
_material.EnableEmission = true;
}
private void OnTriggerExit(Collider other)
{
_material.EnableEmission = false;
}
}
```
这样就可以实现在触发碰撞时物体自发光的效果了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)