- Home /
Flame Thrower - Particle Collision Help
Hello,
I'm working on a flame thrower turret, and I basically have two main scripts attached to the turret, and a damage receiver script attached to the enemy.
One of the scripts on the turret (SmoothLookAt), controls the rotation of the turret when an enemy walks into range.
The other one (FlameController), controls the particle emitter (the flame) and sends the damage to the damage receiver attached to the enemy - which is what I am having a problem with.
I want to follow the FPS tutorial (unity) closely (as my other turrets use the same format), I want to be able to do something like this:
something.SendMessageUpwards("ApplyDamage", flameDamage, SendMessageOptions.DontRequireReceiver);
So I want FlameController to receive if the particles have hit any game objects (enemy), and if so - send the damage to the receiver on the enemy.
I've looked at function OnParticleCollision - but thats a receiver, which means I'll have to place it in my DamageReciever, right?
Well anyway, here are my scripts:
FlameController:
private var flameDamage : float = 0.2; var flameRange : float; var flameOn : boolean = false; var flameParticle : ParticleEmitter;
//ExternalSmooth = gameObject.GetComponentInChildren(SmoothLookAt);
function Update () {
var ExternalSmooth : SmoothLookAt;
ExternalSmooth = gameObject.GetComponentInChildren(SmoothLookAt);
// controls flame particle
if(ExternalSmooth.target){
flameOn = true;
flameParticle.particleEmitter.emit = true;
}
else{
flameOn = false;
flameParticle.particleEmitter.emit = false;
}
// somehow send damge from particle.collider.SendMessageUpwards("ApplyDamage", flameDamage, SendMessageOptions.DontRequireReceiver);
}
DamageReceiver: (from the FSP tutorial - modified slightly)
static var Health; var hitPoints = 10.0; var MaxHitPoints = 10.0; var HealthBarSize : float = 0.2; var detonationDelay = 0.0; var explosion : Transform; var deadReplacement : Rigidbody; var credits = 100; private var damageByFlames : float = 0.6;
public var Dead = false;
/* function OnParticleCollision(){ print("HIT BY FIRE!!"); //currently recieving - i do not want that - i flamecontroller to send damge report
} */
function Start(){ Health = Controller.EnemyHealth; hitPoints = Health; MaxHitPoints = Health; }
function Update(){ var script : HealthBar; script = gameObject.GetComponentInChildren(HealthBar); script.Health = hitPoints; script.MaxHealth = MaxHitPoints; script.MaxBar = HealthBarSize; }
function ApplyDamage (damage : float) { // We already have less than 0 hitpoints, maybe we got killed already? if (hitPoints <= 0.0) //credits += 100; return;
hitPoints -= damage;
if (hitPoints <= 0.0) {
// Start emitting particles
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = true;
Invoke("DelayedDetonate", detonationDelay);
}
}
function DelayedDetonate () { //GetComponent(SmoothLookAt).LookAtDead = false; BroadcastMessage ("Detonate"); //Dead = true; }
function Detonate () { // Destroy ourselves Credits.Credits += 90; Controller.EnemyAdding ++; Destroy(gameObject);
// Create the explosion
if (explosion)
Instantiate (explosion, transform.position, transform.rotation);
// If we have a dead barrel then replace ourselves with it!
if (deadReplacement) {
var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);
// For better effect we assign the same velocity to the exploded barrel
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
// If there is a particle emitter stop emitting and detach so it doesnt get destroyed
// right away
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter) {
emitter.emit = false;
emitter.transform.parent = null;
}
}
// We require the barrel to be a rigidbody, so that it can do nice physics @script RequireComponent (Rigidbody)
Sorry for the essay, but I'm trying to make my self clear. I would appreciate any help.
Thanks!
Summary:
Any way to do it like this:
OnParticleCollision.SendMessageUpwards("ApplyDamage", flameDamage, SendMessageOptions.DontRequireReceiver);
Answer by Statement · Apr 03, 2011 at 05:59 PM
I've looked at function OnParticleCollision - but thats a receiver, which means I'll have to place it in my DamageReciever, right?
From the docs: This message is sent to all scripts attached to the WorldParticleCollider and to the Collider that was hit.
I'd put this code in FlameController.js, given it sits on the same object as the WorldParticleCollider, or add a component with this code to the WorldParticleCollider (During Start, if it hasn't got one).
function OnParticleCollision (other : GameObject) {
other.SendMessageUpwards("ApplyDamage", flameDamage,
SendMessageOptions.DontRequireReceiver);
}
Depending on your hierarchy of objects you might want change SendMessageUpwards to SendMessage or BroadcastMessage, it's up to you.
ParticleDamage.js
var damage : float;
function OnParticleCollision (other : GameObject) {
other.SendMessageUpwards("ApplyDamage", flameDamage,
SendMessageOptions.DontRequireReceiver);
}
FlameController.js
private var flameDamage : float = 0.2; private var externalSmooth : SmoothLookAt; private var particleDamage : ParticleDamage;
var flameRange : float; var flameOn : boolean = false; var flameParticle : ParticleEmitter;
// Cache externalSmooth externalSmooth = gameObject.GetComponentInChildren(SmoothLookAt);
// Set up particleDamage: particleDamage = flameParticle.GetComponent(ParticleDamage); if (!particleDamage) particleDamage = flameParticle.gameObject.AddComponent(ParticleDamage);
particleDamage.damage = flameDamage;
function Update () { if(ExternalSmooth.target){ flameOn = true; flameParticle.emit = true; } else{ flameOn = false; flameParticle.emit = false; } }
Not working :( - $$anonymous$$y SmoothLookAt script and FlameController are both on the same object - in the same hierarchy. Although my particle is within a child of the object that those scripts are attached to
See my updated answer. It will add a ParticleDamage script to the emitter if it doesn't have any.
Because my FlameController is not on particle system - is there a way to use OnParticleCollision to read off a variable that is set as a particle? So: var paricleSystem : ParticleEmitter; then: ParticleEmitter.OnParticleCollision?
We access the particle emitter through your variable flameParticle. It sits on the gameObject that is emitting particles obviously. So what we can do then is add ParticleDamage (see my script above) to the same gameObject with AddComponent. This will handle sending ApplyDamage to any colliders it hit. So you push a script to the right location. (You could always just manually put it on the emitter object as well)
I didnt want to have to create another script to put on my particle system - but hey! - Works anyway - cheers!
Your answer
Follow this Question
Related Questions
how to send message every few seconds? 3 Answers
how do i send and receive messages in a game object? 1 Answer
Trouble sending message... 2 Answers
SendMessage every second? 2 Answers
Collision script 1 Answer