200 lines
6.3 KiB
C#
200 lines
6.3 KiB
C#
//using Assets.Script.Base;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
[RequireComponent(typeof(ParticleSystem))]
|
||
public class ParticleMove : MonoBehaviour
|
||
{
|
||
[Tooltip("移动类型point,为到目标位置,Circle为绕目标旋转")]
|
||
public MoveTypeEnum MoveType;
|
||
|
||
[Tooltip("point时为目标位置,Circle时为圆心点")]
|
||
public Transform TargetPosition;
|
||
|
||
[Tooltip("角度为90,270分别顺逆时间转")]
|
||
[Range(0, 360)]
|
||
public float P = 1;
|
||
|
||
public float MoveTime;
|
||
|
||
private float Speed;
|
||
float Distance;
|
||
ParticleSystem particleSys;
|
||
ParticleSystem.Particle[] particleArray;
|
||
|
||
|
||
public enum MoveTypeEnum
|
||
{
|
||
Point,
|
||
Circle
|
||
//以后需要啥加啥,先这2种
|
||
}
|
||
|
||
void Start()
|
||
{
|
||
|
||
particleSys = GetComponent<ParticleSystem>();
|
||
|
||
if (particleArray == null || particleArray.Length < particleSys.main.maxParticles)
|
||
particleArray = new ParticleSystem.Particle[particleSys.main.maxParticles];
|
||
|
||
//////Debug.Log((P2 - P1).magnitude);
|
||
|
||
}
|
||
|
||
void LateUpdate()
|
||
{
|
||
|
||
//if (TargetPosition == null)
|
||
//{
|
||
// if (m_Target != null)
|
||
// {
|
||
// TargetPosition = m_Target.transform;
|
||
// }
|
||
//}
|
||
|
||
if (TargetPosition != null)
|
||
{
|
||
MoveFunc();
|
||
}
|
||
}
|
||
|
||
void MoveFunc()
|
||
{
|
||
|
||
var numParticlesAlive = particleSys.GetParticles(particleArray);
|
||
|
||
for (int i = 0; i < numParticlesAlive; i++)
|
||
{
|
||
if (particleSys.shape.shapeType == ParticleSystemShapeType.SkinnedMeshRenderer || particleSys.shape.shapeType == ParticleSystemShapeType.MeshRenderer)
|
||
{
|
||
|
||
MainMoveFuncByMesh(i);
|
||
|
||
}
|
||
else
|
||
{
|
||
MainMoveFunc(i);
|
||
}
|
||
|
||
}
|
||
particleSys.SetParticles(particleArray, numParticlesAlive);
|
||
|
||
}
|
||
void MainMoveFunc(int i)
|
||
{
|
||
if (MoveType == MoveTypeEnum.Point)
|
||
{
|
||
if (particleSys.main.simulationSpace == ParticleSystemSimulationSpace.Local)
|
||
{
|
||
//realyPos.position = transform.TransformPoint(particleArray[i].position);
|
||
Vector3 normalize = (transform.InverseTransformPoint(TargetPosition.position) - particleArray[i].position).normalized;
|
||
Distance = (transform.InverseTransformPoint(TargetPosition.position) - particleArray[i].position).magnitude;
|
||
Speed = Distance / MoveTime;
|
||
// particleArray[i].position = new Vector3(particleArray[i].position.x*normalize.x, particleArray[i].position.y * normalize.z, particleArray[i].position.z * normalize.y) * Speed ;
|
||
particleArray[i].position += normalize * Speed;
|
||
|
||
}
|
||
if (particleSys.main.simulationSpace == ParticleSystemSimulationSpace.World)
|
||
{
|
||
movePointByWorld(i);
|
||
}
|
||
|
||
}
|
||
if (MoveType == MoveTypeEnum.Circle)
|
||
{
|
||
if (particleSys.main.simulationSpace == ParticleSystemSimulationSpace.Local)
|
||
{
|
||
Vector3 normalize = (transform.InverseTransformPoint(TargetPosition.position) - particleArray[i].position).normalized;
|
||
|
||
Distance = (transform.InverseTransformPoint(TargetPosition.position) - particleArray[i].position).magnitude;
|
||
Speed = Distance / MoveTime;
|
||
Vector2 v21 = RotationMatrix(new Vector2(normalize.x, normalize.y), P);
|
||
|
||
particleArray[i].position += new Vector3(v21.x, v21.y, normalize.z) * Speed;
|
||
|
||
}
|
||
if (particleSys.main.simulationSpace == ParticleSystemSimulationSpace.World)
|
||
{
|
||
moveCircleByWorld(i);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
void MainMoveFuncByMesh(int i)
|
||
{
|
||
if (MoveType == MoveTypeEnum.Point)
|
||
{
|
||
if (particleSys.main.simulationSpace == ParticleSystemSimulationSpace.Local)
|
||
{
|
||
//particleSys.simulationSpace = ParticleSystemSimulationSpace.World;
|
||
|
||
ParticleSystem.MainModule mainModule = particleSys.main;
|
||
mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
|
||
}
|
||
if (particleSys.main.simulationSpace == ParticleSystemSimulationSpace.World)
|
||
{
|
||
movePointByWorld(i);
|
||
}
|
||
|
||
}
|
||
if (MoveType == MoveTypeEnum.Circle)
|
||
{
|
||
if (particleSys.main.simulationSpace == ParticleSystemSimulationSpace.Local)
|
||
{
|
||
//particleSys.simulationSpace = ParticleSystemSimulationSpace.World;
|
||
|
||
ParticleSystem.MainModule mainModule = particleSys.main;
|
||
mainModule.simulationSpace = ParticleSystemSimulationSpace.World;
|
||
}
|
||
if (particleSys.main.simulationSpace == ParticleSystemSimulationSpace.World)
|
||
{
|
||
moveCircleByWorld(i);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
void moveCircleByWorld(int i)
|
||
{
|
||
Vector3 normalize = (TargetPosition.position - particleArray[i].position).normalized;
|
||
|
||
Distance = (TargetPosition.position - particleArray[i].position).magnitude;
|
||
Speed = Distance / MoveTime;
|
||
Vector2 v21 = RotationMatrix(new Vector2(normalize.x, normalize.z), P);
|
||
|
||
particleArray[i].position += new Vector3(v21.x, normalize.y, v21.y) * Speed;
|
||
}
|
||
void movePointByWorld(int i)
|
||
{
|
||
//这行只是测试初始点位置用的
|
||
//realyPos.position = transform.InverseTransformPoint(particleArray[i].position).normalized;
|
||
Vector3 Devil = TargetPosition.position - particleArray[i].position;
|
||
|
||
if (Mathf.Abs(Devil.x) >= 0.01f && Mathf.Abs(Devil.y) >= 0.01f && Mathf.Abs(Devil.z) >= 0.01f)
|
||
{
|
||
Vector3 normalize = Devil.normalized;
|
||
Distance = (TargetPosition.position - particleArray[i].position).magnitude;
|
||
Speed = Distance / MoveTime;
|
||
particleArray[i].position += normalize * Speed;
|
||
|
||
}
|
||
}
|
||
|
||
private Vector2 RotationMatrix(Vector2 v, float angle)
|
||
{
|
||
var x = v.x;
|
||
var y = v.y;
|
||
var sin = Mathf.Sin(Mathf.PI * angle / 180);
|
||
var cos = Mathf.Cos(Mathf.PI * angle / 180);
|
||
var newX = x * cos + y * sin;
|
||
var newY = x * -sin + y * cos;
|
||
return new Vector2((float)newX, (float)newY);
|
||
}
|
||
}
|