• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Kith · Mar 24, 2010 at 08:14 PM · enemyprojectiledamagemessage

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...

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image e.bonneville · Mar 24, 2010 at 08:20 PM 0
Share

No biggie... I've asked about 12 in the last week! :)

avatar image e.bonneville · Mar 24, 2010 at 08:36 PM 0
Share

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.

avatar image Not showing my name · Apr 22, 2010 at 05:33 PM 0
Share

Very,Very,Very,Very good question.

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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 &lt;= 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! ;)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KvanteTore · Mar 24, 2010 at 08:36 PM 0
Share

Seems like OP prefers javascript

avatar image e.bonneville · Mar 24, 2010 at 08:37 PM 0
Share

Yeah, so do I, I couldn't write it in C#, to be honest, lol.

avatar image Kith · Mar 24, 2010 at 08:38 PM 0
Share

lol I do...only because I know it :-P

avatar image e.bonneville · Mar 24, 2010 at 08:40 PM 0
Share

:O... Do you give lessons?

-Just kidding!

avatar image
2
Best Answer

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);
 }

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kith · Mar 24, 2010 at 08:36 PM 0
Share

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

avatar image e.bonneville · Mar 24, 2010 at 08:41 PM 0
Share

yeah, i though you were doing JS, my bad...

avatar image
2

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

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KvanteTore · Mar 24, 2010 at 08:34 PM 0
Share

heh. looks like i'm not the only one trolling the forums tonight. voted up for company :)

avatar image e.bonneville · Mar 24, 2010 at 08:41 PM 0
Share

Just thinking that, i'm low on rep.... LoL

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Damaging Enemy strangely not working. 0 Answers

Damage script is screwed up...? what to do? 1 Answer

Player Character Health 2 Answers

Instantiate a projectile on key press "F" key / OnMouseDown! Help!!! 2 Answers

Attack selected Enemy Turn Based Combat 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges