Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Particle Simulate Local And World Space question

Discussion in 'Scripting' started by Dark_Seth, Sep 21, 2015.

  1. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    130
    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.



    My Code
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var laser : ParticleSystem ;
    5. var particles : ParticleSystem.Particle[];
    6. var target : Transform;
    7. var endpoint : Vector3;
    8.  
    9.  
    10. function Update(){
    11.  
    12. }
    13.  
    14. function LateUpdate() {
    15.  
    16. InitializeIfNeeded();
    17.   var lenght = laser.GetParticles(particles);
    18.   transform.LookAt(target);
    19.   endpoint = target.position;
    20.  
    21.   for (var i = 0; i < lenght; i++) {
    22.  
    23.  
    24.  
    25.   if(Vector3.Distance(target.position, particles[i].position) < 2f) {
    26.   particles[i].lifetime = -0.1f; //Kill the particle
    27.  
    28.  
    29.   }
    30.  
    31.   }
    32.  
    33.   laser.SetParticles(particles, lenght);
    34.  
    35.  
    36.  
    37.   }
    38.  
    39.  
    40. function InitializeIfNeeded() {
    41.  if (laser == null)
    42.  laser = this.GetComponent.<ParticleSystem>();
    43.  
    44.  if (particles == null || particles.Length < laser.maxParticles)
    45.  particles = new ParticleSystem.Particle[laser.maxParticles];
    46. }
    47.  
     
  2. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    130
    Got it sorted.

    Thanks
     
  3. techoffice

    techoffice

    Joined:
    May 31, 2018
    Posts:
    1
    How did u do it?
     
  4. Mark-Oudshoorn

    Mark-Oudshoorn

    Joined:
    Oct 10, 2022
    Posts:
    1
    Here we sit, still waiting for him to answer