- Home /
Rocket projectile damage not applied on collision (message not sent/received)
I am trying to get a rocket launcher to apply damage on collision between the rocket and an object tagged with "Enemy".
The objects are colliding, but the enemy object is not taking any damage (message is not being sent?), and my debug logs (collision detected, damage received) are not showing up either.
Both the rocket and enemy objects have a rigid body and a capsule collider.
Here's my "RocketDamage" script, attached to the Rocket prefab:
#pragma strict
var damage = 25;
function OnTriggerEnter(collider:Collider)
{
if (collider.CompareTag("Enemy"))
{
collider.gameObject.SendMessage("ApplyDamage", damage);
Debug.Log("Collision detected!");
}
}
and my DamageController script (attached to enemy object):
#pragma strict
@script RequireComponent(Rigidbody)
var hitPoints = 100;
var explosionDelay = 0.0;
var explosion : Transform;
function ApplyDamage(damage : float) {
if (hitPoints <= 0.0)
{
return;
}
hitPoints -= damage;
Debug.Log(hitPoints + " - HP remaining.");
if (hitPoints <= 0.0)
{
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = true;
Invoke("DelayedExplosion", explosionDelay);
}
}
function DelayedExplosion() {
BroadcastMessage("Detonate");
}
function Detonate() {
Destroy (gameObject);
if (explosion) {
Instantiate (explosion, transform.position, transform.rotation);
}
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter) {
emitter.emit = false;
particleEmitter.transform.parent = null;
}
}
Would appreciate any ideas on why this is happening. Thanks for your time!
Is the object tagged as Enemy? Check what tag you are hitting by adding this before the conditional (if statement) :
Debug.Log("Collision detected! tag = " + collider.gameObject.tag);
another one of the basics, do you have a collider with "isTrigger" box ticked on the RocketPrefab
Answer by Abidus · Mar 18, 2014 at 02:25 AM
I actually managed to get the functionality to work with OnCollisionEnter instead of OnTriggerEnter. I think the collider on the rocket was not set to be a Trigger, which is why OnTriggerEnter was not functioning properly.
Your answer
Follow this Question
Related Questions
On Trigger Enter, Collide with object, specific collision 1 Answer
Trigger Spawning? 1 Answer
My rockets don't do damage? 1 Answer
Colliding two GameObjects 1 Answer
Area of Damage for a Blink of an Eye 3 Answers