unity3d--粒子系统


简介

这是一款基于粒子系统学习开发的小程序,使玩家通过按钮控制粒子呈现不同的效果

游戏运行效果

游戏开始运行,五种不一样颜色地粒子位于正五边形的顶点。
在这里插入图片描述
点击向左倾斜,给所有粒子添加一个向左的力场,粒子向左倾斜,雪花效果不会改变。
在这里插入图片描述
点击向右倾斜后恢复原状,再点击三次向右倾斜,使倾斜角度增大。
在这里插入图片描述

游戏玩法

玩家通过点击向左倾斜和向右倾斜两个按钮,控制施加在粒子上的力场,控制粒子呈现出不同的效果。

游戏代码介绍

flare : 一个 ParticleSystem 的子类,实现了左风和右风的控制,继承自 Monobehavior 类,定义了leftwind和rightwind两个控制函数,控制粒子的力场方向。
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class flare : MonoBehaviour
{
    ParticleSystem particleSystem;
    ParticleSystem.ForceOverLifetimeModule forceMode;
    ParticleSystem.ColorOverLifetimeModule colorMode;
    int windPower;

    void Start()
    {
        particleSystem = GetComponent<ParticleSystem>();
        forceMode = particleSystem.forceOverLifetime;
        colorMode = particleSystem.colorOverLifetime;
    }

    public void LeftWind()
    {
        ParticleSystem.MinMaxCurve temp = forceMode.x;
        temp.constantMax += -0.3f;
        forceMode.x = temp;
    }

    public void RightWind()
    {
        ParticleSystem.MinMaxCurve temp = forceMode.x;
        temp.constantMax += 0.3f;
        forceMode.x = temp;
    }
}

UserGUI : 一个 Monobehavior 的子类,提供 GUI 按钮用于控制多个 flare 类的实例的风向。定义五团粒子flare1-5,定义按钮位置大小和功能
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UserGUI : MonoBehaviour
{

    public flare flare1, flare2, flare3,flare4, flare5;

    void Start()
    {

    }

    void Update()
    {

    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 150, 60, 30), "向左倾斜"))
        {
            flare1.LeftWind();
            flare2.LeftWind();
            flare3.LeftWind();
            flare4.LeftWind();
            flare5.LeftWind();
        }

        if (GUI.Button(new Rect(Screen.width-60, 150, 60, 30), "向右倾斜"))
        {
            flare1.RightWind();
            flare2.RightWind();
            flare3.RightWind();
            flare4.RightWind();
            flare5.RightWind();
        }
    }
}

代码链接

github

视频链接

bilibili

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值