简体   繁体   中英

Unity2D - How to move and rotate particle system around a circle?

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.

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM