粒子效果
1. 简单粒子制作
- 按参考资源要求,制作一个粒子系统,参考资源
- 使用 3.3 节介绍,用代码控制使之在不同场景下效果不一样
2. 实现过程
-
本次作业要求较为简单,还有相关的参考教程,需要实现一个带有光晕的魔法球。整个粒子可以分为两部分,即光晕(多个)和星光。
-
首先制作例子主体部分–光晕,光晕的中间部分的粒子不会移动,所以设置其speed为0,shape设置为Box或Sphere,让光晕填充整个粒子,显得饱满。
-
设置光晕粒子(Light)的Color over Lifetime组件,光晕由于受四周的光照影响,在实际中,在光晕从出生到消失的时候,肯定是无中生有,那么一切光从黑暗中来最后消失与黑暗,所以最外部的颜色肯定是黑色。
-
设置光晕粒子(Light)的其它参数。
-
设置星光粒子(Particle)的 Color over Lifetime组件参数,星光的特征,也是无中生有,还有就是出生的时候颜色应该和闪光球的颜色一致,然后越来越淡(白色),最后消失于黑暗中(黑色)。
-
设置星光粒子的其他参数。
-
最后将这两种粒子组合到一个空的对象上。
-
星光粒子和光晕粒子的Renderer组件设置相同
-
将成品进行预制,最后通过一个代码加载到场景中:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickHandle : MonoBehaviour
{
public GameObject paricle;
private int speed = 0;
// Start is called before the first frame update
void Start()
{
speed = 0;
//加载预制资源
paricle = ParticleSystem.Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/simple"));
}
// Update is called once per frame
void Update()
{
if (paricle != null && paricle.transform.position.y > -3)
{
paricle.transform.Translate(new Vector3(0, -2, 0) * speed * Time.deltaTime);
}
else if (paricle != null && paricle.GetComponent<ParticleSea>() == null)
{
Destroy(paricle);
this.gameObject.AddComponent<ParticleSea>();
}
if (Input.GetButtonDown("Fire1"))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hits;
hits = Physics.RaycastAll(ray);
for (int i = 0; i < hits.Length; i++)
{
RaycastHit hit = hits[i];
if (hit.collider.gameObject != null)
{
speed = 2;
}
}
}
}
}