Search Unity

Particle system

Discussion in '2D' started by artemstrelnik, Oct 22, 2019.

  1. artemstrelnik

    artemstrelnik

    Joined:
    Oct 14, 2019
    Posts:
    5
    Здравствуйте ! Есть небольшой проект, по управлению частицами. Управляются они двумя кастомными классами. Задача состоит в следующем мне необходимо привязать источник частиц к объекту "A" в свою очередь конечную точку куда стремятся все частицы к объекту "B". P.s(закрепить поток частиц между двумя башнями). В конечном итоге хотелось бы получить результат при котором изменяя расстояние между башням , изменялась и длина потока частиц.
    Какими методами можно воспользоваться , что бы создать такую логику поведения системы частиц?
    Благодарю!
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    vakabaka, Ted_Wikman and karl_jones like this.
  3. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    I'll have to give this a go with Google Translate too.

    I can see that a colleague has helped you with your question. Please be mindful that the forums only allow posts in English. I'm not aware of any Russian speaking Unity forums myself but it may be researching this if you have any more questions but or not able to communicate in English, as we wouldn't be able to assist here without the use of Google Translate, and that my just confuse things more.

    --

    Я вижу, что коллега помог вам с вашим вопросом. Пожалуйста, помните, что на форумах допускаются сообщения только на английском языке Я не знаю ни одного русскоязычного форума Unity, но он может исследовать это, если у вас есть еще вопросы, но вы не можете общаться на английском языке, поскольку мы не смогли бы помочь здесь без использования Google Translate, и что мои просто все путают.
     
    vakabaka and richardkettlewell like this.
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Is it something like particle attractor ?

    if the forcefild will not help (I have never used it, then you can try with scripts).

    ok, I don't think, the script is good. Just as a try to look at the possibility to control the particles trough the script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Attractor : MonoBehaviour
    6. {
    7.     public ParticleSystem particleSys;
    8.     ParticleSystem.Particle[] particles;
    9.  
    10.     private void Start()
    11.     {
    12.         particles = new ParticleSystem.Particle[particleSys.main.maxParticles];
    13.     }
    14.  
    15.     public void Update()
    16.     {
    17.         int particlesCount = particleSys.GetParticles(particles);
    18.  
    19.         for (int i = 0; i < particlesCount; i++)
    20.         {
    21.             Vector3 direction = transform.position - particles[i].position;
    22.  
    23.             if (direction.sqrMagnitude < 0.5f)
    24.             {
    25.                 particles[i].remainingLifetime = 0;
    26.             }
    27.             else
    28.             {
    29.                 particles[i].velocity = direction.normalized * particleSys.main.startSpeed.constant;
    30.             }
    31.        
    32.         }
    33.         particleSys.SetParticles(particles);
    34.     }
    35. }
    I think, depended on the particle system, it can be better just to change the main settings from the particle system if the end position was moved and don't calculate every particle.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChangeParticle : MonoBehaviour
    6. {
    7.     public ParticleSystem particleSys;
    8.     Vector3 tmpPosition;
    9.  
    10.     private void Start()
    11.     {
    12.         tmpPosition = transform.position;
    13.         ChangeDirection();
    14.     }
    15.  
    16.     private void Update()
    17.     {
    18.         if (tmpPosition != transform.position)
    19.         {
    20.             ChangeDirection();
    21.         }
    22.     }
    23.  
    24.     void ChangeDirection ()
    25.     {
    26.         Vector3 direction = transform.position - particleSys.transform.position;
    27.  
    28.         float flyTime = direction.magnitude / particleSys.main.startSpeed.constant;
    29.         var tmpTime = particleSys.main;
    30.         tmpTime.startLifetime = flyTime;
    31.  
    32.         direction = direction.normalized * particleSys.main.startSpeed.constant;
    33.         var tmpVelocity = particleSys.velocityOverLifetime;
    34.         tmpVelocity.x = direction.x;
    35.         tmpVelocity.y = direction.y;
    36.         tmpVelocity.z = direction.z;
    37.  
    38.         tmpPosition = transform.position;
    39.     }
    40. }
     
    Last edited: Oct 22, 2019
    Ledesma099 likes this.