Reputation: 492
How I can set particle emitting mesh in "Particle System"(Shuriken) component? Searching in google returns me tutorials only for legacy particle system.
UPD: @gamedevelopmentgerm answered to my question:
If you want to control all aspects of particle system in runtime then you must use legacy particles.
Upvotes: 1
Views: 6712
Reputation: 1334
...
UP1. Oh, I see the comment below. No, you can't change mesh shape from script though once I have seen a workaround for this problem. It was something about setting up an empty mesh as ParticleSystem emission shape an then change the mesh geometry from script.
UP2. Ok, I see further details. If you want to simulate fire propagation then I recommend you to consider ParticleSystem.Emit method instead of any emission shape whatsoever. I recommend it because with ParticleSystem.Emit you will be able to control emission shape in much more flexible way.
In fact, you may use ParticleSystem.Emit to emit particles from different mesh instances as well.
using UnityEngine;
using System.Collections;
[RequireComponent( typeof( ParticleSystem ) )]
public class CustomParticleSystem : MonoBehaviour
{
#region Properties
public Mesh[] EmissionShapes;
public float EmissionShapeSwitchSpeed = 0.5f;
public float EmissionRate = 10f;
#endregion
float _shapeIndex = 0f;
float _timeToEmission = 0f;
ParticleSystem _particleSystem;
float EmissionPeriod { get { return 1f / EmissionRate; } }
void Awake ()
{
_particleSystem = GetComponent<ParticleSystem>();
_particleSystem.emissionRate = 0f;
_timeToEmission = EmissionPeriod;
}
void Start ()
{
}
void Update ()
{
_shapeIndex += EmissionShapeSwitchSpeed * Time.deltaTime;
if( _shapeIndex > EmissionShapes.Length-1 ) _shapeIndex -= (EmissionShapes.Length-1);
_timeToEmission -= Time.deltaTime;
if( _timeToEmission <= 0f )
{
_timeToEmission = EmissionPeriod - _timeToEmission;
Mesh currentShape = EmissionShapes[(int)_shapeIndex];
int triangleIndex = Random.Range( 0, currentShape.triangles.Length/3 );
int vertexIndex0 = currentShape.triangles[triangleIndex*3];
int vertexIndex1 = currentShape.triangles[triangleIndex*3+1];
int vertexIndex2 = currentShape.triangles[triangleIndex*3+2];
Vector3 v0 = currentShape.vertices[vertexIndex0];
Vector3 v1 = currentShape.vertices[vertexIndex1];
Vector3 v2 = currentShape.vertices[vertexIndex2];
Vector3 n0 = currentShape.normals[vertexIndex0];
Vector3 n1 = currentShape.normals[vertexIndex1];
Vector3 n2 = currentShape.normals[vertexIndex2];
float u = Random.Range( 0f, 1f );
float v = Random.Range( 0f, 1f );
float w = Random.Range( 0f, 1f );
float uvw = u+v+w;
u /= uvw;
v /= uvw;
w /= uvw;
Vector3 randomPosition = v0*u + v1*v + v2*w;
Vector3 normalAtRandomPosition = n0*u + n1*v + n2*w;
randomPosition = this.transform.localToWorldMatrix.MultiplyPoint( randomPosition );
normalAtRandomPosition = this.transform.localToWorldMatrix.MultiplyVector( normalAtRandomPosition );
_particleSystem.Emit(
randomPosition,
normalAtRandomPosition * _particleSystem.startSpeed,
_particleSystem.startSize,
_particleSystem.startLifetime,
_particleSystem.startColor
);
}
}
}
Upvotes: 2