c#语言使用Unity粒子系统制作手雷爆炸

 更新时间:2022年04月25日 17:46:43   作者:极客范儿  
这篇文章主要为大家介绍了Unity的粒子系统由粒子发射器、粒子动画器、粒子渲染器组成,通过使用一或两个纹理多次绘制,创造一个混沌的效果,通过复习粒子系统做一个手雷和实弹投掷现场

一、创建地形

1、GameObject ->3D Object-> Terrain,创建带有地形属性的平面

2、Terrain-〉最后一个工具(Terrain Settings)->Set Resolution-〉SetHeightmap resolution ,把地形设置为500*500

3、调整视图

  • Hierarchy->Camera,选择默认的摄像机
  • Hierarchy->Camera->Inspector->Position->X=250,Y=100,Z=-250, Game中能看到一个梯形状的平面(以后随时调整XYZ值得到最佳效果)

4、设置地形高度

SetHeight

Height->200

5、设置地形背景

二、应用资源包

1、下载资源

从unity官网中下载好所需要的资源包

“Third Person Controller - Basic Locomotion FREE.unitypackage”

“Environment Pack Free Forest Sample.unitypackage”

2、导入资源

Assets->Import Package->Custom Package->依次选中Package包

3、Project ->All Prefabs->将显示人像的ThirdPersonController_LITE预制件拖拉到Scene窗口中的地面上(参数可以选用默认值),同时将与ThirdPersonController_LITE相隔3个的vThirdPersonCamera_LITE预制件拖拉到Scene窗口中(该预制件能跟踪人体的运动,替代默认的Camera)。

4、选中Hierarchy-〉Untitled->Main Camera,在Inspector下的Main Camera左边的小框中的勾选去掉,让Main Camera失效。

三、制作手雷

1、导入SceneShot.unitypackage

2、将Grenade置入场景中

3、Collisions.cs脚本程序(1) //拖拽到ThirdPersonController_LITE上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collisions : MonoBehaviour
{
    public static int GRENADE_AMMO = 0;
    // Start is called before the first frame update
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    { 
        RaycastHit hit;
        //check if we are collidering
        float rayCastLength = 5;
        Vector3 offset = new Vector3(0,0.5f,0);
        if (Physics.Raycast(transform.position+offset, transform.forward,out hit ,rayCastLength ))
        {
            //...with a door
            if (hit.collider.gameObject.tag == "Door")
            {
                //open the door
                hit.collider.gameObject.GetComponent<Animation>().Play("Door-open");    
            }
        }
        Debug.DrawRay(transform.position + offset, transform.forward, Color.red);
    }
    void OnTriggerEnter(Collider hit)
    {
        if (hit.gameObject.tag == "CrateGrenades")
        {
            Debug.Log("hit");
            //destory the ammo box
            Destroy(hit.gameObject);
            //add ammo to inventory
            GRENADE_AMMO += 200;
            print("YOU NOW HAVE " + GRENADE_AMMO + " GRENADES");
        }
    }
}

在这里插入图片描述

4、Shooting.cs脚本程序 //拖拽到ThirdPersonController_LITE上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
    // Start is called before the first frame update
    float  speed = 3;
    Vector3 offset = new Vector3(0, 1.0f, 0f);
    [Tooltip("gp")]
    public GameObject GrenadePrefab;
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        Screen.lockCursor = true;
        //find out if a fire button is pressed
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("shoot");
            if (Collisions.GRENADE_AMMO > 0)
            {
                //create the prefab
                    GameObject grenade = Instantiate(GrenadePrefab, this.transform.localPosition+ offset+ transform.forward, Quaternion.identity);
                //add force to the prefab
                grenade.GetComponent<Rigidbody>().AddForce(transform.forward * 20, ForceMode.Impulse);
                Collisions.GRENADE_AMMO--;
                print("YOU NOW HAVE " + Collisions.GRENADE_AMMO + " GRENADES");
            }
        }
    }
}

5、GrenadeScript.cs脚本程序 //将脚本拖拽到grenade预制体上

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrenadeScript:MonoBehaviour
{
    private float creationTime;
    public GameObject explosionPrefab;
    int lifetime = 3;
    // Start is called before the first frame update
    void Start()
    {
    }
    void Awake()
    {
        Debug.Log("count");
        creationTime = Time.time;
    }
    // Update is called once per frame
    void Update()
    {
        if (Time.time > (creationTime + lifetime))
        {
            //Destroy(this.gameObject);
            Instantiate(explosionPrefab, transform.position, Quaternion.identity);
        }
    }
}

6、在当前工程文件的Assets目录下,建一个Audio文件夹,将手榴弹爆炸音效文件Grenade.mp3,拷贝到Audio文件夹中

点击Project->Assets->Audio->Grenade音乐文件,在Inspector面板最下方有一个运行按钮(右下方的Grenade栏右边的箭头),可以倾听音效

Project-〉Prefabs-〉explosion- >在Inspector中点击Add Component ->Audio->Audio Source,在Inspector中的Audio Source下有AudioClip,

选Grenade音乐组件单击Hierarchy-〉vThirdPersonCamera_LITE,查看Inspector面板上的组件属性,Audio Listener起到在游戏运行时侦听场景中的音乐效果

运行,按键盘的左健,发射手榴弹,手榴弹落地后消失,在产生火焰爆炸特效的同时,可以侦听到手榴弹爆炸的声音特效

在这里插入图片描述

在这里插入图片描述

7、最终效果

在这里插入图片描述

以上就是c#语言使用Unity粒子系统制作手雷爆炸的详细内容,更多关于Unity粒子系统制作手雷爆炸的资料请关注脚本之家其它相关文章!

相关文章

  • c# 实现轮询算法实例代码

    c# 实现轮询算法实例代码

    这篇文章主要介绍了c# 实现轮询算法实例代码的相关资料,这里附有实例代码,具有一定的参考价值,需要的朋友可以参考下
    2016-12-12
  • C# FileStream简单介绍和使用

    C# FileStream简单介绍和使用

    这篇文章主要为大家详细介绍了C# FileStream基本功能和使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • C#连接Informix数据库的问题

    C#连接Informix数据库的问题

    这篇文章主要介绍了C#连接Informix数据库的问题,本文给大家介绍的非常详细,对大家的工作或学习具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • DevExpress实现禁用TreeListNode CheckBox的方法

    DevExpress实现禁用TreeListNode CheckBox的方法

    这篇文章主要介绍了DevExpress实现禁用TreeListNode CheckBox的方法,在项目开发中有应用价值,需要的朋友可以参考下
    2014-08-08
  • C#自定义处理xml数据类实例

    C#自定义处理xml数据类实例

    这篇文章主要介绍了C#自定义处理xml数据类,涉及C#针对XML的打开、读写等常用操作,并将其封装进一个类中以便于调用,非常具有实用价值,需要的朋友可以参考下
    2015-03-03
  • C#实现ComboBox自动匹配字符

    C#实现ComboBox自动匹配字符

    本文介绍C#如何实现ComboBox自动匹配字符1.采用CustomSource当做提示集合2. 直接使用下拉列表中的项作为匹配的集合,需要了解的朋友可以参考下
    2012-12-12
  • unity实现屏幕上写字效果

    unity实现屏幕上写字效果

    这篇文章主要为大家详细介绍了unity实现屏幕上写字效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • C# winfroms使用socket客户端服务端的示例代码

    C# winfroms使用socket客户端服务端的示例代码

    这篇文章主要为大家详细介绍了C# winfroms使用socket客户端服务端的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • C# SDK实现百度云OCR的文字识别功能

    C# SDK实现百度云OCR的文字识别功能

    这篇文章主要为大家详细介绍了C# SDK实现百度云OCR的文字识别功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • C#使用正则表达式隐藏手机号中间四位为*

    C#使用正则表达式隐藏手机号中间四位为*

    这篇文章主要介绍了C#使用正则表达式隐藏手机号中间四位为*的相关资料,需要的朋友可以参考下
    2017-06-06

最新评论