I have two types of particle, one for each player:
- Player 1 (Red Particle)
- 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:
Image2:
How do I call these particles?