Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by s-joy-laube · Nov 09, 2020 at 03:37 PM · particlesparticlesystememit

'Swap' a particle from one system to another, keeping it's position

Hello, i am new to particles, so if there is a better way to do this, please point it out. I want a player to attract some particles and have them orbit around him. For this i have created an empty orbitParticleSystem around the player. The position of particles that go within a certain range of the player is saved and applied to a newly emitted particle in the orbitSystem with EmitParams.position. Now I have two problems:

  1. with ParticleSystem.EmitParams.applyShapeToPosition = false, the position is applied perfectly, but ofcourse the new orbit shape isn't applied. With ParticleSystem.EmitParams.applyShapeToPosition = true, the shape is applied properly but it just spawns somewhere on the orbit, not in the given position. I have a video to illustrate this: https://youtu.be/BV7NT8-Xsxs

  2. the old particle is not destroyed, even though it's lifetime is set to -1. By default the particles in that system have an infinite lifetime, but i thought it can be overwritten.

Here is my code:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ParticleAttractor : MonoBehaviour
 {
     public ParticleSystem[] pSystems;
     public float speedFactor = 2f;
     public float orbitStrength = 1f;
     private ParticleSystem.Particle[] particles;
     private int numParticlesAlive;
     public ParticleSystem orbitPSystem;
     private ParticleSystem.Particle particle;
 
     private void Start()
     {
         if (pSystems == null || pSystems.Length == 0)
         {
             Debug.LogError("No particle System assigned");
         }
     }

     void FixedUpdate()
     {    
             foreach (ParticleSystem pSystem in pSystems)
             {
                 //update particles:
                 initializeParticles(pSystem);
                 numParticlesAlive = pSystem.GetParticles(particles);
             
                 for (int i = 0; i < numParticlesAlive; i++)
                 {
                     //lerp particles to this transform:
                     particles[i].position = Vector3.Lerp(particles[i].position, this.transform.localPosition, Time.deltaTime / speedFactor);
                     Vector3 distanceVector = this.transform.localPosition - particles[i].position;
 
                 // if a particle is in range:
                 if (distanceVector.magnitude <= 10)
                 {
                     var emitOverride = new ParticleSystem.EmitParams();
                     emitOverride.position = particles[i].position;
                     // when this is false, the position is applied perfectly (but doesn't behave like it should). If it's true, the particle is spawned somewhere on the new emittor-shape:
                     emitOverride.applyShapeToPosition = true;
                     particle = particles[i]; 
                     //this doesn't seem to delete the particle :
                     particle.remainingLifetime = -1;
                     orbitPSystem.Emit(emitOverride, 1);
                     Debug.LogError("swapped a particle");
                 }                 
             }
             
                 // Apply the particle changes to the Particle System:
                 pSystem.SetParticles(particles, numParticlesAlive);
             }           
     }
 
     private void initializeParticles(ParticleSystem _pSystem)
     {    
         if (particles == null || particles.Length < _pSystem.main.maxParticles)
             particles = new ParticleSystem.Particle[_pSystem.main.maxParticles];
     }    
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

146 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to force a particle system emit from every vertex a mesh have? 3 Answers

Particles emit in the same direction twice if they hit a collider 0 Answers

Detect the start of a particle burst emission 0 Answers

Manually and efficiently placing thousands of particles 0 Answers

Emit Random Particles? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges