Search Unity

Particles: change single particle color

Discussion in 'General Graphics' started by riba78, Jul 17, 2019.

  1. riba78

    riba78

    Joined:
    Feb 16, 2018
    Posts:
    33
    Hi guys,
    I've a question.
    Consider an object moving on a surface that has different colors.
    This object emits particles.
    I'd like to color the emitted particles according to the colors of the surface where the object move over.

    I've tried to change the start color of a particles system but it changes all the emitted particles.

    Is there a way to change the color of the new single emitted particles keeping the color of the old ones?

    Thanks in advance.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Afaik changing the start color should be fine. If you're testing this in the editor, make sure to disable the Resimulate option.
     
    ghiboz and richardkettlewell like this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ParticleStartColorTest : MonoBehaviour {
    6.  
    7.     ParticleSystem ps;
    8.     Vector3 pos;
    9.  
    10.     void Start () {
    11.         ps = GetComponent<ParticleSystem>();
    12.         pos = transform.position;
    13.     }
    14.  
    15.     void Update () {
    16.         if (ps == null)
    17.         {
    18.             enabled = false;
    19.             return;
    20.         }
    21.  
    22.         Vector3 newPos = pos;
    23.         newPos.x = Mathf.Sin(Time.timeSinceLevelLoad) * 3f;
    24.         transform.position = newPos;
    25.  
    26.         var main = ps.main;
    27.         main.startColor = Random.ColorHSV(0f, 1f, 0f, 1f, 0.5f, 1f, 1f, 1f);
    28.     }
    29. }
    upload_2019-7-17_10-7-37.png