I’m spawning leaves gently falling from a tree, and I’m using velocity over lifetime to make them look as if they’re lightly floating downwards… like they are swaying left to right as they fall
When the leaf particle collides with the ground (any object within the “ground” layer) I want to disable the velocity over lifetime for that 1 particle… or something similar (need to kill velocity somehow)
the idea is that the leaf falls from the tree, hits the ground, then fades out over some time
It may not be the best of ways to accomplish this, but it’s the best I could think of.
Basically, this script gets all of the particles, then raycasts down from each particles’ position to look for ground. If it finds it, then we consider this particle ‘grounded’. Since we already have the reference to the particle, simply change it’s velocity.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
ParticleSystem m_system;
void Start()
{
m_system = GetComponent<ParticleSystem>();
}
void FixedUpdate()
{
ParticleSystem.Particle[] particleList = new ParticleSystem.Particle[m_system.particleCount];
int l = m_system.GetParticles(particleList);
for(int i = 0; i < l; i++)
{
if (onGround(particleList*))*
{
particleList*.velocity = new Vector3(0, 0, 0);*
}
}
m_system.SetParticles(particleList, l);
}
bool onGround(ParticleSystem.Particle p)
{
Vector3 pos = transform.TransformPoint(p.position); //Converting to world coords because the position by default is in local.
RaycastHit hit;
Ray r = new Ray(pos, Vector3.down);
if (Physics.Raycast(r, out hit, 0.1f))
{
if (hit.collider.gameObject.tag == “Ground”)
return true;
else
return false;
}
return false;
}
}
Particle Collision has a dampen parameter to define how much speed the particle should lose on collision. Set this to 1 and the particles will stop on collision. [71302-capture.jpg|71302]