- Home /
'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:
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
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];
}
}
Your answer
Follow this Question
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