0
votes

I'm trying to instantiate a rain particle system to follow a circular path. The clouds rotate and move around a circular planet perfectly fine. The problem arises when the particle system is instantiated, it almost never has the correct rotation, no matter where it is spawned. I tried making the particles a child of the cloud, but that didn't work either.

Here is where I instantiate it - this is applied to any given cloud:

void UpdateCloudState(){
        if(isRaining == true){
            Sprite.color = new Color(.2f,.2f,.2f,1f);
            if(!isInstantiated){
                rain = Instantiate(RainParticles, this.transform.localPosition, Quaternion.identity) as GameObject;
                rain.GetComponent<ParticleSystem>().Play();
                isInstantiated = true;
            }
        }
        else{
            isInstantiated = false;
            Sprite.color = new Color(1f,1f,1f,1f);
            rain.GetComponent<ParticleSystem>().Stop();
            Destroy (rain, 1.0f);
            CloudMovement.speed = originalSpeed;
        }
    }

UPDATE: After extensive testing i have figured out that the problem is SOLELY when I instantiate a PARTICLE SYSTEM through a script.

-A fix that i discovered: I made an empty prefab the parent of the particle system and then that was made all into 1 prefab.

-I applied the Cloud Movement script (the one that moves and rotates around a circle) to that new prefab and it now works

-HOWEVER, it still does NOT work through scripting when I instantiate it.

1
You posted this before.. Unity2D - How to move AND rotate a gameobject around a circle in a 2D game? and in fact having seen the graphic before I know I already answer this and how toBugFinder
@BugFinder yes i posted something similar but the behavior for particle systems seems completely differentScott
its not, its just an object, if you have it setup, it will do exactly what you tell it.BugFinder
Please edit the question to include the configuration of your particle systemRuzihm
@Ruzihm Done, I added the most important settings from the particle systemScott

1 Answers

0
votes

I found an alternate solution for my specific problem. I still can't figure out how to make it work by instantiating it. I had to make an empty gameobject with the particle system a child of it, then attach that new prefab to the cloud prefab. and in the script:

void Start()
    {
        Sprite = gameObject.GetComponent<SpriteRenderer>();
        originalSpeed = this.gameObject.GetComponent<CloudMovement>().speed;
        StartCoroutine(StormCoroutine());
        stormLength = 10;
        currentPlanet = GameObject.Find("BigPlanet").transform;
        ////////////////////////////////////////////////////////////
        /////////////////This is the first step of the fix//////////
        ////This line sets 'rain' to the particle system/////
        rain = gameObject.transform.GetChild(0).GetChild(0).gameObject;
    }

void UpdateCloudState(){
        if(isRaining == true){
            Sprite.color = new Color(.2f,.2f,.2f,1f);
            if(!isInstantiated){
                ////////////////////////////////////////////////////////////
                ////This if statement accesses the rain object and plays the particles when necessary///
                rain.GetComponent<ParticleSystem>().Play();
                isInstantiated = true;
            }
        }
        else{
            isInstantiated = false;
            Sprite.color = new Color(1f,1f,1f,1f);
            rain.GetComponent<ParticleSystem>().Stop();
            this.gameObject.GetComponent<CloudMovement>().speed = originalSpeed;
        }
    }