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

2022-07-15,,,

一、创建地形

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#语言使用Unity粒子系统制作手雷爆炸.doc》

下载本文的Word格式文档,以方便收藏与打印。