- Home /
Particle System emitting at wrong location
Hi! I've got a BulletPrefab with a Particle System attached to it (as child). Hitting the FireButton instatiates clones of my bullet. Everything's working fine so far. I want the Particle System to emit whenever a bullet hits a collider (obstacles, other players and so on) at the exact position where the bullet hits said colliders. This only works for the first time. I start the game, shoot at an enemy1 and the particle effect appears right there where it collides with enemy1. Shooting enemy2 results in the particle effect appearing at the position from enemy1. That is obviously not what I want. I need the particle effect to appear on different locations, exactly there where the Bullet hits an collider.
Here's the code I wrote. It's attached to the BulletPrefab.
void OnTriggerEnter2D(Collider2D otherCollider){
GameObject.FindGameObjectWithTag ("Splash").GetComponent<ParticleSystem> ().transform.parent = null;
GameObject.FindGameObjectWithTag ("Splash").GetComponent<ParticleSystem> ().Play ();
}
The bullets are destroyed as soon as they hit a collider and to prevent the destruction of the particle System as well I need to detach it from its parent.
So here's my question: What can I do to move the Particle Systems location to wherever the new instatiated bullet hits a collider?
Thanks in advance! And if you need more information, just let me know.
Answer by MicDaRoc · Aug 03, 2015 at 03:17 PM
I've got it!
Instead of having a Particle System as child of the ShotPrefab, I created a Particle System Prefab. In the shotscript (which is attached to the shotPrefab) I wrote this little piece of code:
public GameObject particlePrefab; public Transform shot1;
void OnTriggerEnter2D(Collider2D otherCollider){
Instantiate( particlePrefab, shot1.position, shot1.rotation);
}
In the Inspector just assign shot1 as your bullet Prefab and there you go. Thanks Bryckout for the help! Two thumps up!
Answer by OrdinaryDev83 · Aug 03, 2015 at 02:10 PM
Attach this to bullet prefab:
var ps : GameObject;
function Update(){
var hit : RaycastHit2D;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast2D(ray,hit){
var go = Instantiate(ps,hit.position,hit.rotation);
Destroy(this.gameObject);
}
}
Next create a prefab for particles. Attach this to particlePrefab:
function Update(){
if(!this.GetComponent(ParticleSystem).IsAlive){
Destroy(this.gameObject);
}
}
Not tested, Have fun.
Thanks for your answer. Where and how do I call the particlePrefab?
Give me you "shooting" script please, if you have one.
if (Input.GetButton ("Fire_P" + animString) && Time.time > nextFire) {
nextFire = Time.time + fireRate;
InstantiatedObj = (GameObject)Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
InstantiatedObj.GetComponent<ShotScript> ().PlayerOwner = this.gameObject;
Destroy (InstantiatedObj, 3f);
var bulletPrefab : GameObject; //The bullet prefab
if (Input.GetButton ("Fire_P" + animString) && Time.time > nextFire) {
nextFire = Time.time + fireRate;
InstantiatedObj = Instantiate (bulletPrefab, shotSpawn.position, shotSpawn.rotation);
}
Try this :) with script and instruction i gave to you.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Trouble with triggers and colliders 1 Answer
Nullreference? 1 Answer
How to write a script for a gameObject that refers to all children? 2 Answers
Bullet Damage on different Limbs 1 Answer