How to Instantiate a particle system at a certain prefab's position?
So i am working on a game where the player (ball) rolls through the level and collects coins. I want to add a vfx i made using the particle system at the the coins position when it is collected.How do i do that? The game is actually an endless runner type game and the levels are generated as the the player moves forward and each level is actually a pre-made prefab which has obstacles and coins. So how do i add effect on the coins position that was just collected? Here is the code :
using UnityEngine; using System.Collections; using UnityEngine .UI ;
public class ScoreManager : MonoBehaviour {
private float score = 0.0f;
private int DifficultyLevel=1;
private int MaxDifficultyLevel = 20;
private int ScoreToNextLevel = 2;
public Text YourScoreText;
private bool IsInvincible ;
public Text ScoreText;
public Animator GameoverAnim;
public Renderer Ball;
public Material RedBall;
public Material YellowBall;
public GameObject coinfx;
// Use this for initialization
void Start () {
IsInvincible = false;
}
// Update is called once per frame
void Update ()
{
if (score >= ScoreToNextLevel && IsInvincible ==false)
LevelUp ();
score += Time.deltaTime;
ScoreText.text = ((int)score).ToString ();
}
void LevelUp()
{
if (DifficultyLevel == MaxDifficultyLevel)
return;
ScoreToNextLevel *= 2;
DifficultyLevel++;
GetComponent <PlayerController > ().SetSpeed (DifficultyLevel );
}
void OnTriggerEnter(Collider trig)
{
if (trig.gameObject.tag == "Obstacle" && IsInvincible == false)
{
//YouLoseCanvas.SetActive(true);
//Time.timeScale = 0;
StartCoroutine(gameover());
}
else if (trig.gameObject.tag == "InvincibleCoin")
{
StartCoroutine(InvincibilityActivate());
}
else if (trig.gameObject.tag == "Coin")
{
Instantiate(coinfx, );
score = score + 5;
ScoreText.text = ((int)score).ToString();
Destroy(trig.gameObject);
}
}
IEnumerator gameover()
{
GameoverAnim.SetTrigger ("GameOver");
YourScoreText.text = "You Scored : " +((int)score).ToString ();
if(PlayerPrefs .GetFloat ("Highscore") < score )
PlayerPrefs.SetFloat ("Highscore", score);
yield return new WaitForSeconds (1);
Time.timeScale = 0;
Debug.Log("GAME OVER !!");
}
IEnumerator InvincibilityActivate()
{
IsInvincible = true;
Ball.material = RedBall;
GetComponent <PlayerController > ().SetSpeed (20 );
yield return new WaitForSeconds (5);
Ball.material = YellowBall;
GetComponent <PlayerController > ().SetSpeed (DifficultyLevel );
IsInvincible = false;
} }
Answer by Cherno · Sep 11, 2016 at 07:06 PM
Instantiate(coinfx, trig.transform.position, Quaternion.identity) as GameObject;
The Scripting API is your friend :)
This Worked .! Thanks a Lot :) I know this was a very silly question but i am new to unity and coding in general :P Also thanks for the Scripting API link :)
Answer by iabulko · Sep 12, 2016 at 01:06 AM
Add to coin prefab a child gameobject called "CoinFX" that already has a component of ParticleSystem with setup effect. Now - when you collect coin: Play coin fx particles and do not destroy object for now (to not destroy fx), just turn off the renderer. Eventually you can destroy it after effect will end
trig.Find("CoinFX").GetComponent<ParticleSystem>().Play();
(trig.GetComponent<Renderer>() as MonoBehaviour).enabled = false;
Your answer
Follow this Question
Related Questions
Gameobjects instantiating in the wrong position 0 Answers
Wrong position (always 0,0,0) when Instantiate prefab. 1 Answer
How can I change position of instantiate objects (clones)? 1 Answer
How to Instantiate decals where particles from particle system hits? 1 Answer
2D Unity Instantiate Ragdoll, same Transform as Sprites ( Head, Torso, LArm,RArm,LLeg,RLeg) 2 Answers