c#語言使用Unity粒子系統(tǒng)制作手雷爆炸
一、創(chuàng)建地形
1、GameObject ->3D Object-> Terrain,創(chuàng)建帶有地形屬性的平面
2、Terrain-〉最后一個工具(Terrain Settings)->Set Resolution-〉SetHeightmap resolution ,把地形設(shè)置為500*500
3、調(diào)整視圖
- Hierarchy->Camera,選擇默認(rèn)的攝像機(jī)
- Hierarchy->Camera->Inspector->Position->X=250,Y=100,Z=-250, Game中能看到一個梯形狀的平面(以后隨時調(diào)整XYZ值得到最佳效果)
4、設(shè)置地形高度
SetHeight
Height->200
5、設(shè)置地形背景
二、應(yīng)用資源包
1、下載資源
從unity官網(wǎng)中下載好所需要的資源包
“Third Person Controller - Basic Locomotion FREE.unitypackage”
“Environment Pack Free Forest Sample.unitypackage”
2、導(dǎo)入資源
Assets->Import Package->Custom Package->依次選中Package包
3、Project ->All Prefabs->將顯示人像的ThirdPersonController_LITE預(yù)制件拖拉到Scene窗口中的地面上(參數(shù)可以選用默認(rèn)值),同時將與ThirdPersonController_LITE相隔3個的vThirdPersonCamera_LITE預(yù)制件拖拉到Scene窗口中(該預(yù)制件能跟蹤人體的運(yùn)動,替代默認(rèn)的Camera)。
4、選中Hierarchy-〉Untitled->Main Camera,在Inspector下的Main Camera左邊的小框中的勾選去掉,讓Main Camera失效。
三、制作手雷
1、導(dǎo)入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預(yù)制體上
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、在當(dāng)前工程文件的Assets目錄下,建一個Audio文件夾,將手榴彈爆炸音效文件Grenade.mp3,拷貝到Audio文件夾中
點擊Project->Assets->Audio->Grenade音樂文件,在Inspector面板最下方有一個運(yùn)行按鈕(右下方的Grenade欄右邊的箭頭),可以傾聽音效
Project-〉Prefabs-〉explosion- >在Inspector中點擊Add Component ->Audio->Audio Source,在Inspector中的Audio Source下有AudioClip,
選Grenade音樂組件單擊Hierarchy-〉vThirdPersonCamera_LITE,查看Inspector面板上的組件屬性,Audio Listener起到在游戲運(yùn)行時偵聽場景中的音樂效果
運(yùn)行,按鍵盤的左健,發(fā)射手榴彈,手榴彈落地后消失,在產(chǎn)生火焰爆炸特效的同時,可以偵聽到手榴彈爆炸的聲音特效
7、最終效果
以上就是c#語言使用Unity粒子系統(tǒng)制作手雷爆炸的詳細(xì)內(nèi)容,更多關(guān)于Unity粒子系統(tǒng)制作手雷爆炸的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法
這篇文章主要介紹了DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法,在項目開發(fā)中有應(yīng)用價值,需要的朋友可以參考下2014-08-08C# winfroms使用socket客戶端服務(wù)端的示例代碼
這篇文章主要為大家詳細(xì)介紹了C# winfroms使用socket客戶端服務(wù)端的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02