Particle Simulate Local And World Space question

Guys

I need your help.
The following script kills the particle when it get to the target. When I set my particle System to Emulate Local Space it can’t calculate the particle position in World Space but my energy beam behaves like it should ( Direction wise but goes past the target).
If I set the particles to World Space. It dies at the target cause the distance gets calculated in word space which is correct.
What I want to do is simulate in Local Space ( Energy Beam looks behaves nice) But use the particles Word position to calculate the distance.
Please see Video to clarify what I am trying to say.

var laser : ParticleSystem ;
var particles : ParticleSystem.Particle[];
var target : Transform;
var endpoint : Vector3;
 
 
function Update(){
 
}
 
function LateUpdate() {
 
InitializeIfNeeded();
  var lenght = laser.GetParticles(particles);
  transform.LookAt(target);
  endpoint = target.position;
 
  for (var i = 0; i < lenght; i++) {
 
 
 
  if(Vector3.Distance(target.position, particles*.position) < 2f) {*

particles*.lifetime = -0.1f; //Kill the particle*

}

}

Actually it even says it in the [API docs for Particle.position][1]. You can use [Transform.InverseTransformPoint()][2] to get the world position in local space :slight_smile:

var targetLocal = transform.InverseTransformPoint(target.position);
...
if (Vector3.Distance(targetLocal, particles*.position) < 2f)*

Also, it’s a small thing but whenever you only compare distances ( Vector3.Distance(a, b) < x ) and you don’t really need to know the actual distance in units, it’s usually better to use properties / functions like Vector3.sqrMagnitude that don’t do the last step of the Pythagoras formula to calculate the length of the distance vector (square root) which is a bit performance heavy.

// particle emitter is at(0,0) in this object, so this is also the distance vector to target
var targetLocal = transform.InverseTransformPoint(target.position);

for (var i = 0; i < lenght; i++) {

  • // distance vector between particle and target*
    _ var distToTarget : Vector3 = particles*.position - targetLocal;_
    _
    // compare the square of distance to square of your limit (2f)_
    if (distToTarget.sqrMagnitude < 4f) …
    Or even better, since you need to compare if a particle has gone past a certain point (if it’s really fast, it might slip through your current check where it needs to be close to target) you could use dot product.
    It’s a fast calculation that checks whether 2 vectors point in similar direction ( dot > 0 ) or opposite direction ( dot < 0 ).
    _
    // particle emitter is at(0,0) in this object, so this is also the distance vector to target*_
    var targetLocal = transform.InverseTransformPoint(target.position);
    * for (var i = 0; i < lenght; i++) {*
    var distToTarget : Vector3 = particles*.position - targetLocal;*
    // if vector from particle source to target points in the same direction as
    // vector from target to particle, it means the particle has passed the target
    * if (Vector3.Dot(distToTarget, targetLocal) > 0) …*
    *[1]: http://docs.unity3d.com/ScriptReference/ParticleSystem.Particle-position.html*_
    *[2]: http://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html*_