c#语言使用Unity粒子系统制作 * *
作者:极客范儿 时间:2021-10-11 11:13:46
一、创建地形
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、最终效果
来源:https://blog.csdn.net/Prototype___/article/details/124372413
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 线程的优先级(setPriority)案例详解
![](https://img.aspxhome.com/file/2023/8/59168_0s.png)
SSM框架搭建图文教程(推荐)
![](https://img.aspxhome.com/file/2023/1/59061_0s.png)
java编写ftp下载工具
详解mysql插入数据后返回自增ID的七种方法
![](https://img.aspxhome.com/file/2023/3/62043_0s.png)
Java遗传算法之冲出迷宫
Java 单例模式的实现资料整理
C#中的Socket编程详解
![](https://img.aspxhome.com/file/2023/4/88374_0s.gif)
使用C# Winform应用程序获取网页源文件的解决方法
Java采用循环链表结构求解约瑟夫问题
Android中TimePicker与DatePicker时间日期选择组件的使用实例
![](https://img.aspxhome.com/file/2023/0/84740_0s.jpg)
分布式医疗挂号系统SpringCache与Redis为数据字典添加缓存
![](https://img.aspxhome.com/file/2023/5/57405_0s.png)
详解Android Flutter中SliverAppBar的使用教程
![](https://img.aspxhome.com/file/2023/7/97867_0s.jpg)
Spring AOP实现权限检查的功能
![](https://img.aspxhome.com/file/2023/5/69285_0s.png)
Java Spring Security认证与授权及注销和权限控制篇综合解析
![](https://img.aspxhome.com/file/2023/7/92537_0s.png)
android视频截屏&手机录屏实现代码
![](https://img.aspxhome.com/file/2023/7/87537_0s.png)
详解Java单元测试之Junit框架使用教程
![](https://img.aspxhome.com/file/2023/8/61788_0s.png)
Flutter实现矩形取色器的封装
![](https://img.aspxhome.com/file/2023/9/96009_0s.jpg)
c#的时间日期操作示例分享(c#获取当前日期)
详解Spring Boot Oauth2缓存UserDetails到Ehcache
springboot整合mybatis plus与druid详情
![](https://img.aspxhome.com/file/2023/7/77487_0s.png)