- Home /
Player's projectile communicating with Enemy
Okay, I know I've been asking a ton of questions over the past couple of days, but you've all been incredibly helpful and I don't know where else to turn lol. Basically, I have a few different projectile prefabs with scripts attached to them that control how the projectiles move, and how much damage they do to the enemy once they collides with it. I also have an Enemy prefab that stores how much health the Enemy has.
What I want to do is make a system where when a projectile collides with the enemy, it tells the enemy how much to subtract from it's health. I really don't know where to start. :-. I was looking into SendMessage but I don't know how to use that...at all
Right now, the way I got it to work temporarily was by adding code similar to this in the OnCollisionEnter() function...
var projectileType : String = other.gameObject.tag;
switch (projectileType) {
case "fireball":
lowerHealth(2);
break;
case "poisonball":
lowerHealth(5);
break;
}
In every one of my enemy prefabs. Although it works, it really becomes a pain as I start adding more weapon types and more enemies. It'd be nice if there was a way to just tell the Enemy prefab how much damage it needs to subtract straight from the projectile...
No biggie... I've asked about 12 in the last week! :)
See my answer below - the projectile talks to the bad guy, and sends it the damage message via a "send call up to collider" approach.
Answer by e-bonneville · Mar 24, 2010 at 08:29 PM
EDIT: I take it from your script you're using JS. This script is JS, too, so it should work.
A good start would be to find out which object you are hitting. Like so:
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
This guy sends a message to the collider hit that it got damage - the "ApplyDamage" does that, assuming your bad guy has an "ApplyDamage" function. Next, it sends up the variable amount of damage. Finally, it says "if there's nobody to get this damage, then let it drift off to the big place in the sky" (SendMessageOptions.DontRequireReceiver). Now, if you don't have a function called "ApplyDamage", create one on your bad guy. It should look like this:
function ApplyDamage (damage : float) { // We already have less than 0 hitpoints, maybe we got killed already? if (hitPoints <= 0.0) return;
hitPoints -= damage;
if (hitPoints <= 0.0) {
//Put your "if dead" script here:
}
}
Then, put the "hit.collider" script on your bullet, and ApplyDamage on the enemy, and you should be set to go! Good luck! ;)
Yeah, so do I, I couldn't write it in C#, to be honest, lol.
Answer by KvanteTore · Mar 24, 2010 at 08:21 PM
I suppose you have an Enemy component that stores the health? One way to achieve your goal would be to create a TakeDamage(float amount) method on your Enemy-component, and then on the Projectile component you can send a TakeDamage message to the collided object.
In C#:
public class Enemy : MonoBehaviour { public float Health;
public void TakeDamage(float amount)
{
Health -= amount;
}
}
public class Projectile : MonoBehaviour { public float Damage;
public void OnCollisionEnter(Collision collision)
{
collision.gameObject.SendMessage("TakeDamage", Damage);
}
}
Awesome, this is exactly what I was looking to do. Thanks ^_^, same to you elbon. I would have checked your answer too if I could lol
Answer by qJake · Mar 24, 2010 at 08:22 PM
Create some sort of HP component (or use your existing one - the one that stores the enemy's HP). For this answer, we'll call it HPManager
. Then, you'd want to do something like this:
// C# void OnCollisionEnter(Collision other) { // check here for making sure it was a bullet that hit an enemy, if needed // if(other.gameObject.name.Contains("bullet")) // or something like this
// Get the instance of the script on the enemy and store it in the "hp" variable.
HPManager hp = other.gameObject.GetComponent(typeof(HPManager)) as HPManager;
// Call whatever method on the enemy's script you want to deduct HP.
hp.DeductHP(amount); // or something like this
}
heh. looks like i'm not the only one trolling the forums tonight. voted up for company :)
Your answer
Follow this Question
Related Questions
Damaging Enemy strangely not working. 0 Answers
Rotate object around player when space is pressed. 1 Answer
My enemy wont take damage 0 Answers