iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >c#语言使用Unity粒子系统制作手雷爆炸
  • 581
分享到

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

2024-04-02 19:04:59 581人浏览 泡泡鱼
摘要

目录一、创建地形二、应用资源包三、制作手雷一、创建地形 1、GameObject ->3D Object-> Terrain,创建带有地形属性的平面 2、Terrain-

一、创建地形

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粒子系统制作手雷爆炸的资料请关注编程网其它相关文章!

--结束END--

本文标题: c#语言使用Unity粒子系统制作手雷爆炸

本文链接: https://www.lsjlt.com/news/147320.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c#语言使用Unity粒子系统制作手雷爆炸
    目录一、创建地形二、应用资源包三、制作手雷一、创建地形 1、GameObject ->3D Object-> Terrain,创建带有地形属性的平面 2、Terrain-...
    99+
    2024-04-02
  • c#怎么使用Unity粒子系统制作手雷爆炸
    这篇文章主要介绍“c#怎么使用Unity粒子系统制作手雷爆炸”,在日常操作中,相信很多人在c#怎么使用Unity粒子系统制作手雷爆炸问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”c#怎么使用Unity粒子系统...
    99+
    2023-06-30
  • c#使用Unity粒子实现炮塔发射系统
    目录一、进行粒子效果生成练习1、生成一个空项目2、创建一个粒子系统3、更改粒子系统参数4、Emission属性更改参数5、Shape属性更改参数6、发射管相关参数7、发射管粒子对象二...
    99+
    2024-04-02
  • c#如何使用Unity粒子实现炮塔发射系统
    今天小编给大家分享一下c#如何使用Unity粒子实现炮塔发射系统的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、进行粒子效...
    99+
    2023-06-30
  • 使用C语言制作扫雷游戏
    本篇文章和大家了解一下使用C语言制作扫雷游戏。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。实现扫雷创建俩12*12的字符数组,一个用作放雷,一个呈现给用户2、利用随机数往防雷数组中产生雷3、设计呈现给用户的数组4、利用输...
    99+
    2023-06-15
  • 使用C语言怎么制作一个扫雷游戏
    本篇文章给大家分享的是有关使用C语言怎么制作一个扫雷游戏,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。C语言是什么C语言是一门面向过程的、抽象化的通用程序设计语言,广泛应用于底...
    99+
    2023-06-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作