简单的unity脚本
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
简单的unity脚本粒⼦光环空⽗物体的⼦物体挂⼀个粒⼦,然后挂这个脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleHalo : MonoBehaviour
{
private ParticleSystem particleSys; // 粒⼦系统
private ParticleSystem.Particle[] particleArr; // 粒⼦数组
private CirclePosition[] circle; // 极坐标数组
public int count = 10000; // 粒⼦数量
public float size = 0.03f; // 粒⼦⼤⼩
public float minRadius = 1.0f; // 最⼩半径
private float oldMinRadius;
public float maxRadius = 2.0f; // 最⼤半径
private float oldMaxRadius;
public bool clockwise = true; // 顺时针|逆时针
public float speed = 2f; // 速度
public float pingPong = 0.00001f; // 游离范围
// Start is called before the first frame update
private Mesh mesh;
int meshVerCount = 0;
void Start()
{
particleArr = new ParticleSystem.Particle[count];
circle = new CirclePosition[count];
// 初始化粒⼦系统
//particleSys = this.GetComponent<ParticleSystem>();
particleSys = this.GetComponentsInChildren<ParticleSystem>()[0];
var main = particleSys.main;
main.startSpeed = 0; // 粒⼦位置由程序控制
main.startSize = size; // 设置粒⼦⼤⼩
main.loop = false;
main.maxParticles = count; // 设置最⼤粒⼦量
particleSys.Emit(count); // 发射粒⼦
particleSys.GetParticles(particleArr);
// particleSys.trails.enabled.Equals(true);
// particleSys.trails.
oldMinRadius = minRadius;
oldMaxRadius = maxRadius;
//mesh = this.GetComponent<MeshFilter>().mesh;
//mesh = this.GetComponentInChildren<MeshFilter>().mesh;
RandomlySpread(); // 初始化各粒⼦位置
}
// Update is called once per frame
private int tier = 10; // 速度差分层数
void Update()
{
//RandomlySpread();
if(isChanged())
{
RandomlySpread();
oldMinRadius = minRadius;
oldMaxRadius = maxRadius;
}
// mesh = this.GetComponentInChildren<MeshFilter>().mesh;
// meshVerCount = mesh.vertexCount;
for (int i = 0; i < count; i++)
{
if(i < meshVerCount){
//下标靠前的粒⼦吸附到mesh的顶点上
particleArr[i].position = mesh.vertices[i];
}else{
//光环
if (clockwise) // 顺时针旋转
//circle[i].angle -= 0.1f;
circle[i].angle -= (i % tier + 1) * (speed / circle[i].radius / tier);
else // 逆时针旋转
//circle[i].angle += 0.1f;
circle[i].angle += (i % tier + 1) * (speed / circle[i].radius / tier);
// 粒⼦在半径⽅向上游离
circle[i].time += Time.deltaTime;
circle[i].radius += Mathf.PingPong(circle[i].time / minRadius / maxRadius, pingPong) - pingPong / 2.0f;
// 保证angle在0~360度
circle[i].angle = (360.0f + circle[i].angle) % 360.0f;
float theta = circle[i].angle / 180 * Mathf.PI;
particleArr[i].position = new Vector3(circle[i].radius * Mathf.Cos(theta), 0f, circle[i].radius * Mathf.Sin(theta)); }
}
particleSys.SetParticles(particleArr, particleArr.Length);
}
void RandomlySpread()
{
for (int i = 0; i < count; ++i)
{ // 随机每个粒⼦距离中⼼的半径,同时希望粒⼦集中在平均半径附近
float midRadius = (maxRadius + minRadius) / 2;
float minRate = Random.Range(1.0f, midRadius / minRadius);
float maxRate = Random.Range(midRadius / maxRadius, 1.0f);
float radius = Random.Range(minRadius * minRate, maxRadius * maxRate);
// 随机每个粒⼦的⾓度
float angle = Random.Range(0.0f, 360.0f);
float theta = angle / 180 * Mathf.PI;
// 随机每个粒⼦的游离起始时间
float time = Random.Range(0.0f, 360.0f);
circle[i] = new CirclePosition(radius, angle, time);
particleArr[i].position = new Vector3(circle[i].radius * Mathf.Cos(theta), 0f, circle[i].radius * Mathf.Sin(theta));
}
particleSys.SetParticles(particleArr, particleArr.Length);
}
public bool isChanged()
{
if(oldMinRadius == minRadius && oldMaxRadius == maxRadius)
return false;
return true;
}
}
public class CirclePosition
{
public float radius = 0f, angle = 0f, time = 0f;
public CirclePosition(float radius, float angle, float time)
{
this.radius = radius; // 半径
this.angle = angle; // ⾓度
this.time = time; // 时间
}
}
球体按⽗节点中⼼向外破碎,⽗物体有⼦物体,⼦物体按⽗物体中⼼向外扩散
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallBroken : MonoBehaviour
{
///[Header("nihhhhh")]
public List<Transform> trans;
public float speed = 0.1f;
public float fillTime = 1.0f;
public float myTick =0.0f;
public bool isv;
//Vector3[] tagetPos;
//Vector3[] startPos;
public List<Vector3> basePos;
public List<Vector3> startPos;
public List<Vector3> tagetPos;
//Time time;
// Start is called before the first frame update
void Start()
{
//mesh = this.GetComponent<MeshFilter>().mesh;
trans =new List<Transform>();
for(int i=1;i<this.GetComponentsInChildren<Transform>().Length;i++)
{
trans.Add(this.GetComponentsInChildren<Transform>()[i]);
}
basePos = new List<Vector3>();
startPos = new List<Vector3>();
tagetPos = new List<Vector3>();
for(int i = 0; i < trans.Count; i++){
basePos.Add(trans[i].localPosition);
startPos.Add(trans[i].localPosition);
tagetPos.Add(trans[i].localPosition + trans[i].localPosition);
}
}
// Update is called once per frame
void Update()
{
for(int i = 0; i < trans.Count; i++)
{
trans[i].localPosition = Vector3.Lerp(startPos[i],tagetPos[i],Mathf.Sin(Time.time)); }
}
}。