Unity-Tutorials.com
Professional Tutorials for the Unity 3D Game Engine
Home Forum Free Tutorial Download Trade Links
Contact Us
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Controlling a GameObject Instantiated ParticleSystem Prefab?

 
Post new topic   Reply to topic    Unity-Tutorials.com Forum Index -> Advanced Tutorial - Particles
View previous topic :: View next topic  
Author Message
lampshade



Joined: 25 Jun 2010
Posts: 1

PostPosted: Fri Jun 25, 2010 9:34 pm    Post subject: Controlling a GameObject Instantiated ParticleSystem Prefab? Reply with quote

Hi,

Let me start off my saying that I found your tutorial very insightful, especially the ParticleTrigger scene with Doodad and TriggerBox scripts.

I was wondering if you could help me achieve something a little different.

Currently, I am trying to use the TriggerBox and Doodad concepts for different purposes. In the ParticleTrigger scene, your particles are at a fixed location. What I mean is that when I press "Let it Begin" the particles emit from one location and to proceed toward the box trigger. When I press "Let it reset" the particles fade away by a factor of 0.005, great! Awesome work!

The difference in my comparison is how the particles come into existance. It seems that you have a StartParticles() function that controlls the particle systems in the scene, whereas I am instantiating them based on a hotkey and a trigger that the player can enter.

It seems that I get some errors when using a ParticleAnimator to instantiate my particles with. I get no errors when using a GameObject to instantiate the particles, which is dissapointing knowing that GameObjects have no member defintion for ParticleAnimators, just particleEmitter.

I need to be able to use the varaible that I instantiate the particles with in the same way you are using your ResetParticles() function.

Here is what I have:

When the player instantiates a particlePrefab, particles emit at the players position. What I need to do is for when the player moves, I need the particles to move with the player (like the Doodad script), in the direction the player is going, and also to gradually destroy several of the particles (like the TriggerBox script).

I need to gradually destroy particles in the direction the player moves in...but for now, I am working on gradually destroy particles (fading them out) while holder[4].a is > 0.

The particlePrefab is a public GameObject that I have attached to my player which has a complete Particle System. It instantiates when I want to, and I can see it working, I know that what I did in the inspector worked by looking at the result. However, what I am trying to acheive is your ResetParticles() function for when the player moves (which is a simple distance calculation).

Code:

    //In seperate script file I have
    public static ParticleAnimator myEmissions
    ;

    myEmissions = (ParticleAnimator) Instantiate(particlePrefab, transform.position, Quaternion.identity);
    //End seperate script file
   
    public static ParticleAnimator [] emitters
    ;
    //Call our coroutine to gradually destroy particles

    public static IEnumerator ResetParticles()
   {
      
      holder = new Color[5];
      holder[4].a = 1;
      
   
      while (holder[4].a > 0)
      {
         foreach (ParticleAnimator m_emitter in emitters)
         {
            if (m_emitter)
            {               
               holder = m_emitter.colorAnimation;
               holder[4].a -= .005f;
               m_emitter.colorAnimation = holder;
            }
         }
      }
      foreach (ParticleAnimator m_emitter in emitters)
      {
         if (m_emitter)
         {
            m_emitter.particleEmitter.emit = false;
            m_emitter.particleEmitter.ClearParticles();
         }
      }
      
      yield return 0;
   }



Their are some invalid cast exceptions with:

Code:

if (particlePrefab != null)
            myEmissions = (ParticleAnimator) Instantiate(particlePrefab, transform.position, Quaternion.identity);


but if I use:

Code:


public static GameObject instantiated
;
//...

if (particlePrefab != null)
            instantiated = (GameObject) Instantiate(particlePrefab, transform.position, Quaternion.identity);


I don't get the invalid cast exception..


Again (and hopefully to be more clear) I need to have a ResetParticles() function that uses an instantiated ParticleAnimator variable.


I would like to get this resolved as soon as possible so I can move on to the next step which would be getting the particles to flow around the player after they move from firstly instantiating the particles while the player is on the trigger.

Any help is very much appreciated!! =))
Back to top
View user's profile Send private message
GargerathSunman



Joined: 20 Jul 2009
Posts: 47

PostPosted: Sun Jun 27, 2010 11:04 pm    Post subject: Reply with quote

If what you want access to is the ParticleAnimator, just use instantiated.GetComponent(ParticleAnimator); to gather the animator from the created object. The reason you get an invalid cast exception is more than likely because you can't access the animator directly through the instantiate function. I've seen it return game objects, transforms, and rigidbodies, but never a particle animator.

You could also have a script on each of these instantiated emitters that has a variable for holding the particle animator and just decreases the alpha value by hand.
_________________
Bringing you the best in the simplest way possible.
http://www.unity-tutorials.com
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Unity-Tutorials.com Forum Index -> Advanced Tutorial - Particles All times are GMT - 8 Hours (US PST)
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group