1
\$\begingroup\$

I have two types of particle, one for each player:

  1. Player 1 (Red Particle)
  2. Player 2 (Blue Particle)

When player 1 collides with an obstacle then I want to play the red particle system, and when player 2 collides with an obstacle then I want to play the blue particle system.

Code:

TouchControll.cs

 public GameObject PlayersPartical;        

  void OnTriggerEnter2D(Collider2D other)  //here my player collided with obstacle
    {
        foreach (Transform child in transform)
        {
            if (child.tag == "point")
            {

                //GameObject partical = Instantiate(twopartical[Random.Range(0,1)], transform.position, transform.rotation); 
                //ParticleSystem heal = partical.GetComponent<ParticleSystem>();
                //heal.Play();
                GameObject partical = Instantiate(PlayersPartical, transform.position, transform.rotation);
                ParticleSystem heal = partical.GetComponent<ParticleSystem>();
                heal.Play();

                ObjectPooler.Instance.SpawnFromPool("redandbluepartical", transform.position, Quaternion.identity);
            }
        }
     }

ObjectPooler.cs

    public static ObjectPooler Instance;

    public void Awake()
    {
        Instance = this;
    }

    [System.Serializable]
    public class Pool                 
    {
        public string tag;
        public GameObject prefab1;
        public GameObject prefab2;
        public int size;
    }

    public List<Pool> pools; 

    public Dictionary<string, Queue<GameObject>> pooldictionary;//which gameobject you should be pool

    void Start () {

        pooldictionary = new Dictionary<string, Queue<GameObject>>();  

        foreach(Pool pool in pools) //add a pool in list
        {
            Queue<GameObject> objectpool = new Queue<GameObject>();

            for(int i=0;i<pool.size;i++)
            {
                GameObject obj =  Instantiate(pool.prefab1);
                GameObject obj1 = Instantiate(pool.prefab2);
                obj.SetActive(false);
                obj1.SetActive(false);

                //objectpool.Enqueue(obj);
            }

            pooldictionary.Add(pool.tag, objectpool);
        }
    }

   public GameObject SpawnFromPool(string tag,Vector3 position,Quaternion rotation)  //two tag red and blue partical
    {
        if(!pooldictionary.ContainsKey(tag))
        {
            Debug.Log("doesnot exist" + tag);
            return null;
        }

        GameObject objecttospawn =  pooldictionary[tag].Dequeue();

        //here how to call my trigger

        objecttospawn.SetActive(true);
        objecttospawn.transform.position = position; //pos
        objecttospawn.transform.rotation = rotation; //rotation

        pooldictionary[tag].Enqueue(objecttospawn);
        return objecttospawn; 
    }

Image1:

enter image description here

Image2:

enter image description here

How do I call these particles?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

For your new question: I've changed the pooling system to new one. this one is not generic but I've written it spacial for your case:

Pooling.CS

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public enum PlayerColor
{
    Blue,
    Red
};

public class Pooling : MonoBehaviour
{
    public static Pooling Instance;

    [Header("Particles systems")]
    public GameObject particleObject;

    [Header("Particles systems Materials"), Space(10)]
    public Material greenMaterial;
    public Material redMaterial;

    private List<ParticleSystem> particlesList;
    private ParticleSystemRenderer redrer;

    public void Awake()
    {
        Instance = this;
        particlesList = new List<ParticleSystem>();
    }

    public void PlayParticleFromPool(PlayerColor color, Vector3 position, Quaternion rotation)
    {
        if (particlesList.Count < 1)
        {
            GameObject firstParticleObject = Instantiate(particleObject, position, rotation);
            particlesList.Add(firstParticleObject.GetComponent<ParticleSystem>());
            firstParticleObject.transform.parent = transform;
        }

        for (int i = 0; i < particlesList.Count; i++)
        {
            if (!particlesList[i].isPlaying)
            {
                redrer = particlesList[i].GetComponent<ParticleSystemRenderer>();
                switch (color)
                {
                    case PlayerColor.Blue:
                        redrer.material = greenMaterial;
                        break;
                    default:
                        redrer.material = redMaterial;
                        break;
                }

                particlesList[i].Play();
                return;
            }
        }

        GameObject newParticleObject = Instantiate(particleObject, position, rotation);
        newParticleObject.transform.parent = transform;
        particlesList.Add(newParticleObject.GetComponent<ParticleSystem>());
        redrer = newParticleObject.GetComponent<ParticleSystemRenderer>();
        switch (color)
        {
            case PlayerColor.Blue:
                redrer.material = greenMaterial;
                break;
            default:
                redrer.material = redMaterial;
                break;
        }
        newParticleObject.GetComponent<ParticleSystem>().Play();
    }
}

And I simplified the player script to satisfy this new Pooling system, Replace Old Code with this:

public PlayerColor playerColor;

    void OnTriggerEnter2D(Collider2D other)  //here my player collided with obstacle
    {
        Pooling.Instance.PlayParticleFromPool(playerColor, transform.position, Quaternion.identity);
    }

Step By Step Guide How to use these scripts:

  1. Replace Your Old Pooling system with new one.
  2. Create "Empty Object" and assign the new Pooling script to it.
  3. Now Create Two Material One for Red Player and another for Blue Player.
  4. Setup Blue particle system Material like this: assign texture for blue particles. enter image description here
  5. Setup Red particle system Material like this: assign texture for red particles enter image description here
  6. Now Change Player Color like this:

    enter image description here

7.Select Blue for blue player and select Red for red Player.

  1. At last step set you pooling like this:

enter image description here

I Hope this will work. I've tested it and it did worked for me.

\$\endgroup\$
11
  • \$\begingroup\$ thanks for answer but the problem is always playing blue particle. when red player touch obstacle then plays a blue particle and when a blue player touch obstacle then plays a blue particle. see this: imgur.com/a/1x5N5Gi \$\endgroup\$ Commented Aug 29, 2019 at 11:34
  • \$\begingroup\$ Have you changed player color as i mentioned in setp 6. I checked it and it is working. \$\endgroup\$
    – Swati
    Commented Aug 29, 2019 at 11:39
  • \$\begingroup\$ can u notice my player game object store the script(TouchControll.cs)? how to set step number seven I have only one script. \$\endgroup\$ Commented Aug 29, 2019 at 12:00
  • \$\begingroup\$ You have two players Right and each player has there own player has there own script. so each player setup the color. \$\endgroup\$
    – Swati
    Commented Aug 29, 2019 at 12:09
  • \$\begingroup\$ Ahh you made the two player child of the single parent. and that's why are always getting blue particles.. \$\endgroup\$
    – Swati
    Commented Aug 29, 2019 at 12:13
1
\$\begingroup\$

I don't know with whom this script is attached but I would say this job should be done by player itself and my way of doing this will be like this:

public GameObject playerPartical;

private void OnCollisionEnter(Collision collision)
{
    GameObject particalObject = Instantiate(playerPartical, transform.position, transform.rotation);
    ParticleSystem partical = particalObject.GetComponent<ParticleSystem>();
    partical.Play();
}

As this will be attached to each player then "player 1" will get reference to Red particles and "player 2" will get reference to "Blue particles". Now by this way each player will be only responsible for themselves.

But the best possible solution wold to Object pooling. As i don't know much about your game so i would stop here and if you have any question please let me know.

\$\endgroup\$
9
  • \$\begingroup\$ this code attach to the player. can u give tips for how to work partical with objectpooling. \$\endgroup\$ Commented Aug 29, 2019 at 5:35
  • \$\begingroup\$ I have linked video to object pooling you can learn all about object pooling from there. to visit the video click on the Object pooling in answer section. \$\endgroup\$
    – Swati
    Commented Aug 29, 2019 at 5:37
  • \$\begingroup\$ Also try the code I've posted. \$\endgroup\$
    – Swati
    Commented Aug 29, 2019 at 5:38
  • \$\begingroup\$ each player will be only responsible for themselves exactly. \$\endgroup\$ Commented Aug 29, 2019 at 5:39
  • \$\begingroup\$ From that statement i mean player don't have to find which particle should i play. As each player knows about themselves. \$\endgroup\$
    – Swati
    Commented Aug 29, 2019 at 5:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .