How do you get particles to follow along the terrain?
I'm trying to make a particle system which follows the ground built with unity's terrain. Is there a way to tell the particles to follow the terrain or is it something I'm going to need to make myself by getting the current terrain height and updating the particles as they flow?
Got it working eventually so posting here in case anyone else has the same issue. I have a function which updates particle system position with terrain height and a function to rotate individual particles with the surface normal of the terrain. The 2nd part was much harder than the first.
private void $$anonymous$$oveParticlesOnTerrain(ParticleSystem ps)
{
int numParticlesAlive = ps.GetParticles(m_Particles);
if (m_Particles == null || m_Particles.Length < ps.maxParticles)
m_Particles = new ParticleSystem.Particle[ps.maxParticles];
// Change only the particles that are alive
for (int i = 0; i < numParticlesAlive; i++)
{
RaycastHit raycastHit;
Ray r = new Ray(m_Particles[i].position + Vector3.up * 100f, Vector3.up * -1f);
if (Physics.Raycast(r, out raycastHit, terrainLayer))
{
Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal);
m_Particles[i].axisOfRotation = Vector3.forward;
m_Particles[i].rotation3D = rot.eulerAngles;
m_Particles[i].position = new Vector3(m_Particles[i].position.x,
gravity.currentTerrain.SampleHeight(m_Particles[i].position) + .1f,
m_Particles[i].position.z);
}
}
ps.SetParticles(m_Particles, numParticlesAlive);
}
private void $$anonymous$$oveParticleSystemOnTerrain(ParticleSystem ps)
{
ps.transform.position = new Vector3(ps.transform.position.x,
gravity.currentTerrain.SampleHeight(ps.transform.position) + .1f,
ps.transform.position.z);
}
Hi, thank you! I was looking for a solution to create sand particles, and stumbled upon your post. You use raycasting here - how about performance with, let's say, 10000 particles? Also, why is there "gravity.currentTerrain.SampleHeight" -> you sample heighmap, offset by 0.1, but why "gravity" ? Thank you?
Answer by ash_richie · Mar 17, 2016 at 07:12 PM
just create particles and keep it under your player as child
and itll follow u
Well that works if you want the particles just on the player and not out on the terrain somewhere.