c#怎么使用Unity粒子系统制作手雷爆炸

发布时间:2022-04-26 10:13:55 作者:iii
来源:亿速云 阅读:172

这篇文章主要介绍“c#怎么使用Unity粒子系统制作手雷爆炸”,在日常操作中,相信很多人在c#怎么使用Unity粒子系统制作手雷爆炸问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”c#怎么使用Unity粒子系统制作手雷爆炸”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、创建地形

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

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

3、调整视图

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");
        }
    }
}

c#怎么使用Unity粒子系统制作手雷爆炸

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起到在游戏运行时侦听场景中的音乐效果

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

c#怎么使用Unity粒子系统制作手雷爆炸

c#怎么使用Unity粒子系统制作手雷爆炸

7、最终效果

c#怎么使用Unity粒子系统制作手雷爆炸

到此,关于“c#怎么使用Unity粒子系统制作手雷爆炸”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 怎么在unity制作配置文件
  2. Unity SLua 如何调用Unity中C#方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

unity

上一篇:php如何将空格转换成nbsp符

下一篇:SpringBoot中@ConfigurationProperties注解怎么实现配置绑定

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》