Last active
September 15, 2018 15:23
-
-
Save robotron2084/c09ca3b90a97cdbe2598728710fa2e99 to your computer and use it in GitHub Desktop.
Orient Particles in Unity3d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Collections.Generic; | |
using System.Collections; | |
/** | |
* A simple copy of example code from the manual, modified to orient particles | |
* based on the center of the system. | |
*/ | |
[ExecuteInEditMode] | |
public class OrientParticles : MonoBehaviour | |
{ | |
ParticleSystem m_System; | |
ParticleSystem.Particle[] m_Particles; | |
public float angleOffset = 0.0f; | |
private void LateUpdate() | |
{ | |
InitializeIfNeeded(); | |
// GetParticles is allocation free because we reuse the m_Particles buffer between updates | |
int numParticlesAlive = m_System.GetParticles(m_Particles); | |
// Change only the particles that are alive | |
for (int i = 0; i < numParticlesAlive; i++) | |
{ | |
// use atan2 to calc angle based on position, then convert to degrees. | |
float angle = Mathf.Atan2(m_Particles[i].position.x, m_Particles[i].position.y) * Mathf.Rad2Deg; | |
// add the offset (in case the artwork is rotated, etc.) | |
m_Particles[i].rotation = angle + angleOffset; | |
} | |
// Apply the particle changes to the particle system | |
m_System.SetParticles(m_Particles, numParticlesAlive); | |
} | |
void InitializeIfNeeded() | |
{ | |
if (m_System == null) | |
m_System = GetComponent<ParticleSystem>(); | |
if (m_Particles == null || m_Particles.Length < m_System.maxParticles) | |
m_Particles = new ParticleSystem.Particle[m_System.maxParticles]; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm getting an error (currently Unity 2018.1) for the "m_System.maxParticles" saying:
"'ParticleSystem.maxParticles' is obsolete: 'maxParticles property is deprecated. Use main.maxParticles instead'."
Any advice? Thank you!