- Home /
How to grab, store and reference an objects position on trigger?
The player runs into the coin, the collider is on triggerenter2d. I'm trying to make it so that when the player triggers the coin a coinparticlesystem is instantiated on the coins current position.
The problem is how do I find, store then reference an object's position if it's moving? Here is what I have so far:
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Star")
{
Instantiate(CoinParticleSystem, myCoinPositionLoggingScript.Coin.transform.position, Quaternion.identity);
myscoreCoinParticleEffect.ScoreCoinEFfect();
myGameController.Incrementscore();
myAudioPlayer.PlayOneShot(scoreSFX);
Destroy(other.gameObject);
}
}
Yep exactly right I have a particle system prefab called CoinParticleSystem. I dragged it into the inspector and it played. Play on awake is set to true. But it just won't play when the player runs into the coin here's what I implemented.
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Star")
{
myscoreCoinParticleEffect.ScoreCoinEFfect();
myGameController.Incrementscore();
myAudioPlayer.PlayOneShot(scoreSFX);
Instantiate(CoinParticleSystem, other.transform);
Destroy(other.gameObject);
}
}
I just don't understand why it won't play it seems like everything is correct for it to do so when it's called in the code.
Answer by xxmariofer · Jan 18, 2019 at 10:51 PM
you can access coin position by other.transform.position since other is the coin coillider. Just use Instantiate(CoinParticleSystem, other.transform);
Yes, so I was asking how to store a game object position because I'm trying to instantiate the particle system on the coin when the coin is "collected" (collides with player). I tried what you posted above just now (Instantiate(CoinParticleSystem, other.transform); it did get rid of some errors I was having but it still won't play the particle system. Should I try the other method you described, storing the position then references the position to tell the particle system where to instantiate?
You are instantiating an object, that object once you drop it in the scene it starts emitting particles? cause thats how you spawn an object but you are not initiating the particle system (unless it starts playing in awake)
Ok, I see what you mean. It's not playing because the particle system just enters the scene but isn't commanded to play. The particle system itself is a "burst" meaning that it visually is just an explosion of particles that stops emitting after around half a second. But if I set it to play on awake will it just explode as soon as the game starts or only when it instantiated in the on trigger enter function?
Your answer
Follow this Question
Related Questions
Bullet firing but prefab looking in the wrong direction 1 Answer
Fixing instantiated object in one direction? 0 Answers
Instantiate Problem 2 Answers
I'm having trouble understanding what "does not contain a definition for" means. 3 Answers
The variable othertransform of Prefab has not been assigned 2 Answers