Search Unity

Particles at a character's vertices

Discussion in 'General Graphics' started by MariusRu, Aug 8, 2018.

  1. MariusRu

    MariusRu

    Joined:
    Oct 15, 2015
    Posts:
    33
    Hello,

    I am trying to create a seemingly simple effect: I want to have a particle at each of my character's vertices, so that these particles alone could indicate the character's posture. However, when I set the particles' position to the mesh's vertices', the "particle character" is always in a T-Pose, no matter the pose of the actual character. What may I be missing here, can someone point me in the right direction?



    Here's my code:
    Code (CSharp):
    1.  
    2.     [SerializeField]
    3.     SkinnedMeshRenderer skin;
    4.     Mesh mesh;
    5.  
    6.     ParticleSystem ps1;
    7.     ParticleSystem.MainModule ps1Main;
    8.     ParticleSystemRenderer ps1Ren;
    9.     ParticleSystem.EmissionModule ps1Emi;
    10.     public Material mat1;
    11.     int max; // number of particles to spawn
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         mesh = (Mesh)Instantiate(skin.sharedMesh);
    16.         skin.sharedMesh = mesh;
    17.  
    18.         max = mesh.vertices.Length;
    19.  
    20.         // initialize particle system
    21.         gameObject.AddComponent<ParticleSystem>();
    22.         ps1 = this.GetComponent<ParticleSystem>();
    23.         ps1Main = ps1.main;
    24.  
    25.         ps1Main.loop = false;
    26.         ps1Main.startLifetime = 10;
    27.         ps1Main.maxParticles = max;
    28.         ps1Main.simulationSpace = ParticleSystemSimulationSpace.World;
    29.         ps1Main.startSpeed = 0f;
    30.         ps1Main.startSize = 0.02f;
    31.  
    32.         ps1Ren = this.GetComponent<ParticleSystemRenderer>();
    33.         ps1Ren.material = mat1;
    34.  
    35.         ps1Emi = ps1.emission;
    36.         ps1Emi.enabled = true;
    37.         ps1Emi.rateOverTime = max;
    38.  
    39.     }
    40.    
    41.     void LateUpdate () {
    42.         // mesh = (Mesh)Instantiate(skin.sharedMesh); // no use
    43.  
    44.         ParticleSystem.Particle[] particles = new ParticleSystem.Particle[ps1.particleCount];
    45.         int count = ps1.GetParticles(particles);
    46.         for (int i = 0; i < particles.Length; i++)
    47.         {
    48.             particles[i].position = transform.TransformPoint(mesh.vertices[i]);
    49.         }
    50.         ps1.SetParticles(particles, count);
    51.     }
     

    Attached Files:

  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    MariusRu and richardkettlewell like this.
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,299
    Is the particle system emitter set to mesh mode or skinned mesh? Make sure it is skinned mesh.

    Edit.
    Oh didn't realize you were doing it manually. Yes you need to bake or use the particle system to do it for you with the skinned mesh emissions mode.
     
    MariusRu likes this.
  4. MariusRu

    MariusRu

    Joined:
    Oct 15, 2015
    Posts:
    33
    Thanks Lennart and karl_jones, that worked! For the record, I now begin every LateUpdate()-loop with:

    Code (CSharp):
    1.         mesh = new Mesh();
    2.         skin.BakeMesh(mesh);
     
    richardkettlewell and karl_jones like this.