void Update() { if (splineIterator == null) { Start(); //To avoid problems when we are in editmode } else { float timeToUse = GetComponent <ParticleSystem>().duration; if (customTime > 0) //Use custom time? { timeToUse = customTime; } splineIterator.SetOffsetPercent(myParticleSystem.time / timeToUse); //Get the position Vector3 offsetVector = myTransform.right * offset.x + myTransform.up * offset.y + myTransform.forward * offset.z; myTransform.position = splineTansform.TransformPoint(splineIterator.GetPosition()) + offsetVector; //Set the position if (orientToPath) //Change rotation is needed { myTransform.rotation = Quaternion.LookRotation(splineIterator.GetTangent()); } } }
/// <summary> /// Updates and cahces de particle positions. Call this method if you modify the transform and you are using WorldSpace particles /// </summary> public void UpdatePointArray() { pointArray = spline.GenerateSplinePoints(splineDivisions); if (pointArrayWorld == null) { pointArrayWorld = new Vector3[pointArray.Length]; } pointArray.CopyTo(pointArrayWorld, 0); //Position if (getWorldPosition) { for (int i = 0; i < pointArrayWorld.Length; i++) { pointArray[i] = transform.TransformPoint(pointArray[i]); //Take position rotation and scale into account } } //Speed if (updateSpeed) { if (speedAtPoint == null) { speedAtPoint = new Vector3[pointArray.Length]; } for (int i = 0; i < speedAtPoint.Length; i++) { iterator.SetOffsetPercent((float)i / speedAtPoint.Length); speedAtPoint[i] = iterator.GetTangent(); } } pointArrayLenght = pointArray.Length - 1; //We don't rally need the exact lentght , but the last element index }
/// <summary> /// Calculates the point each animation curve point. /// </summary> private void CalculatePoint(float _point, int _loopNumber, BaseSpline.SplineIterator _iterator, SplineParticles _target, float _offset) { _iterator.SetOffsetPercent(_point); //Calc modifiers float lifeTimeModifier = _target.GetComponent <ParticleSystem>().startLifetime; float loopsModifier = _target.loopNumber; Vector3 currentPosition = _target.transform.TransformPoint(_iterator.GetPosition()); Vector3 tangentAtPoint = (_iterator.GetTangent().normalized); Vector3 velocityAtPoint = Vector3.zero; if (_point == 0 && _target.Spline.WrapMode == BaseSpline.SplineWrapMode.Loop) { _iterator.SetOffsetPercent(1 - _target.pathQuality); previousPoint = _target.transform.TransformPoint(_iterator.GetPosition()); } else if (_point == 0 && _target.Spline.WrapMode != BaseSpline.SplineWrapMode.Loop) { _iterator.SetOffsetPercent(_point + _target.pathQuality); previousPoint = _target.transform.TransformPoint(_iterator.GetPosition()); } velocityAtPoint = (currentPosition - previousPoint).magnitude * tangentAtPoint; previousPoint = currentPosition; velocityAtPoint *= loopsModifier * (1 / _target.pathQuality) / lifeTimeModifier; _target.velocityCurveX.AddKey(_point / _loopNumber + _offset, velocityAtPoint.x); _target.velocityCurveY.AddKey(_point / _loopNumber + _offset, velocityAtPoint.y); _target.velocityCurveZ.AddKey(_point / _loopNumber + _offset, velocityAtPoint.z); }