Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Making a shape with particles

Discussion in 'Scripting' started by Keysaw, Jan 1, 2016.

  1. Keysaw

    Keysaw

    Joined:
    Oct 14, 2015
    Posts:
    19
    Hello !

    I am trying to achieve something, but I have absolutely no idea how to do that...

    I would like to create a shape only with particles (and by script). For exemple, if I have a tree mesh which is invisible, I would like to create a bunch of particles that will stay on the surface of this mesh to make its shape visible.

    Am I clear? And is it easily doable?

    If anyone have any clue on how I can achieve this, It will be really appreciated!

    Thanks in advance!
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    You can select Mesh Renderer as the shape for the particle system with the latest version of Unity which will emit the particles from the shape of the selected object, maybe this will do what you want.
     
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    what Munchy says and set your speed to 0.
     
  4. Keysaw

    Keysaw

    Joined:
    Oct 14, 2015
    Posts:
    19
    Ok, that's nice ! (and pretty simple too, sorry I didn't figured it out)

    BUT: I would like to achieve something a bit more complicated.

    Let's say that each particle represent an object in an array. Is it possible to create theses particles from script instead of making changes from the Particles Emitter options in the inspector ?

    Actually, here's what effect I want to achieve: I create a bunch of particles from script (let's say 500, according to my array's count), and all of these particles will be "attracted" by the mesh, like a magnet, to make its shape.

    So my particles will have an infinite lifetime, they won't appear and disappear after a while.

    Any ideas will be very helpful! Thanks
     
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    i think you better write a script for that behaviour.
    spawn a number of sprite prefabs in a "circle"? around your object tell the to move in the object direction and oncollision freeze their position or make them a child of the object.

    Instantiate random on a sphere.
    move in obj direction:
    Vector3 direction = sprite.transform.position-object.transform.position;
    sprite.transform.position += direction.normalized * Time.deltaTime * speed;
    or
    sprite.transform.position = Vector3.MoveTowards(sprite.transform.position, object.transform.position, Time.deltaTime * speed)
    or
    ...

    to freeze the sprite's position. make a bool orso you set to true on collision and only move if the bool is false.

    hope this makes sense for you.
     
  6. Keysaw

    Keysaw

    Joined:
    Oct 14, 2015
    Posts:
    19
    Wow thanks, I think it's exactly what I was looking for! I will explore it this way.

    One last thing: I was also wondering how I could make a "wiggle" effect for my particle system? I tried a lot of different ways, I can't get it for now... =/
     
  7. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    on it's way to the object or once it's arrived?
     
  8. Keysaw

    Keysaw

    Joined:
    Oct 14, 2015
    Posts:
    19
    More like once it arrived.

    The ideal plan will be that the particles holds around the area, then moves toward the object to "stick" on its mesh (which will then reveal the mesh), and then stays on the mesh but with a smooth wiggle effect.
     
  9. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    so with the particle "frozen" you can activate a small random local offset.
    here's one i use for wiggling some text on a menu (so you probably have to change the while statement)
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using System.Collections;
    4.  
    5. public class RandomMove : MonoBehaviour {
    6.  
    7.     public float min, max, speed;
    8.     Vector2 target;
    9.     void Start ()
    10.     {
    11.         StartCoroutine(Move());
    12.     }
    13.    
    14.     IEnumerator Move ()
    15.     {
    16.         Scene thisScene = SceneManager.GetActiveScene();
    17.         while(thisScene == SceneManager.GetActiveScene())
    18.         {
    19.             target = new Vector2(transform.position.x+Random.Range(min,max),transform.position.y+Random.Range(min,max));
    20.             while(Vector3.Distance(transform.position, target) > 0.1f)
    21.             {
    22.                 transform.position = Vector2.MoveTowards(transform.position, target, speed);
    23.                 yield return null;
    24.             }
    25.             yield return null;
    26.         }
    27.     }
    28. }
     
  10. radinretrogames

    radinretrogames

    Joined:
    Feb 19, 2019
    Posts:
    3
    Hi Keysaw! I'm trying to achieve exactly the same you say here, but I have a problem that I cannot resolve.
    I create a static "starfield" creating a particle system without movement. Then I move each one of the particles(using a loop for) to the transparent object position (the one with the collider collided by the particles). In that moment, I want to stop moving this singular particle (the one that has collided), but in the "OnParticleCollision" I cannot distinguish wich one (wich single particle) I have to stop moving.... It only can report that one of the particles has collided.

    Please... Have you solved this problem?

    Thank you very much.
     
  11. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi, @radinretrogames,

    First off, you probably noticed you necroed a 4 year old thread? :)
    You probably could just generate an array of of particle positions on sphere surface, and then set those to particles? I really don't see where you would need collisions and such if you are trying to make a static starfield.