- Home /
Particle collisions for a NOOB??
hi all, using unity for a while now but have never done collisions with particles, im not sure what im doing wrong/or right,
so i have a player running around with a flamethrower, this is made from a particle systems component, when i hit the enemy with the particle i want it do damage,
i have this code on my object that has the particle emmiter
public var damage = 40;
function OnParticleCollision (collision: GameObject ) { if(collision.transform.tag == "Enemy") {
Debug.Log("Damage");
collision.GetComponent(ZombieStats).Hit(damage);
}
}
and this code is on my enemies
var health : float;
var deathFX : GameObject;
public var hitFX : GameObject; //Hit FX
function Start () {
}
function Update () {
if (health <= 0){
Instantiate(deathFX,transform.position,Quaternion.identity);
Destroy (this.gameObject);
}
}
public function Hit(_damage : int)
{
//Instantiate hit FX
Instantiate(hitFX,transform.position,Quaternion.identity);
//Remove the damage value from the health
health -= _damage;
//If health is less than 0
}
i have the collisions box ticked, its set to world, and send collision messages is also ticked
im sure this is really easy fix for some1, but im all confused, any help or a point in the right direction is appreciated
Answer by GameVortex · Dec 10, 2013 at 12:37 PM
It is not the collision that contains the component ZombieStats but the gameobject inside the collision. You can use collision.gameObject to access the GameObject the particle collided with.
public var damage = 40;
function OnParticleCollision (collision: GameObject ) { if(collision.transform.tag == "Enemy") {
Debug.Log("Damage");
collision.gameObject.GetComponent(ZombieStats).Hit(damage);
}
Also for future reference, please properly describe what your problem with the code is and post any errors you might be getting (also include the line number).
hi Vortex,
for some reason its not letting me post as a comment, so i had to put this in as an answer
thanks for the reply, correct me if im wrong , i thought function OnParticleCollision( collision(name the collision for refrence):GameObject(system type)
so when i get to collision.GetComponent... collision had already been declared as a GameObject,
im still having issues, i change the code to yours, no errors in console, none at run time,
im not getting a debug to say ive hit,
i changed the renderer setting from billboard to mesh.. thinking that would solve it but not such luck
Hi, I am sorry I did not see your comment before. You are absolutely right, I was thinking about OnCollisionEnter which takes a parameter of type Collision. The onparticlecollision is of course already a GameObject. You have probably figured out by now what the problem is, but just for any other that might see this: the issue comes from the fact that it is the receiving GameObject that needs a script with the onparticlecollision callback and not the particle emitter itself.