void UpdateParticles() { if (!ParticleSys) { return; } ParticleSystem.Particle[] ParticleList = new ParticleSystem.Particle[ParticleSys.particleCount]; int NumParticleAlive = ParticleSys.GetParticles(ParticleList); for (int i = 0; i < NumParticleAlive; ++i) { ParticleSystem.Particle Particle = ParticleList[i]; VelocityFieldNode velocityField = new VelocityFieldNode(); bool IsImported = false; if (Curve) { IsImported = GetTargetNode(Particle, ref Curve.VelocityField, ref velocityField); } if (!IsImported) { continue; } Vector3 targetVelocity = velocityField.TargetVelocity * SpeedOnCurve * velocityField.Mag; // get vector from particle position to curve's iteration pos Vector3 toCurveVelocity = (velocityField.TargetPosition - Particle.position).normalized; targetVelocity += toCurveVelocity * ForceToNearestCurve; // apply hierarchy scale for velocity as well targetVelocity.x *= transform.lossyScale.x; targetVelocity.y *= transform.lossyScale.y; targetVelocity.z *= transform.lossyScale.z; ParticleList[i].position = Particle.position + (targetVelocity * Time.deltaTime); ParticleList[i].velocity = Particle.velocity; } ParticleSys.SetParticles(ParticleList, ParticleSys.particleCount); }
public void CalculateVelocityField() { VelocityField.Clear(); Vector3 prevPos = P0.transform.position; for (int c = 1; c <= Resolution; c++) { float t = (float)c / Resolution; Vector3 currPos = CurveMath.CalculateBezierPoint(t, P0.transform.position, P0_Tangent.transform.position, P1_Tangent.transform.position, P1.transform.position); Vector3 currTan = (currPos - prevPos).normalized; float mag = VelocityMagCurve.Evaluate(t); VelocityFieldNode ti = new VelocityFieldNode(); ti.TargetPosition = prevPos; ti.TargetVelocity = currTan; ti.Mag = mag; VelocityField.Add(ti); prevPos = currPos; } }
private bool GetTargetNode(ParticleSystem.Particle Particle, ref List <VelocityFieldNode> velocityField, ref VelocityFieldNode targetInfo) { float minDist = float.MaxValue; VelocityFieldNode node = new VelocityFieldNode(); for (int i = 0; i < velocityField.Count; i++) { float dist = Vector3.Distance(velocityField[i].TargetPosition, Particle.position); if (dist > SearchRadius) { continue; } if (dist < minDist) { minDist = dist; node = velocityField[i]; } } targetInfo = node; return(true); }