- Home /
How to make enemy damage in diferent parts of the object?
Hi, I have another question for this day. I am trying to do many ways to get damage the enemy by shooting diferent parts of the body and I'm not sure if it really works but that why I am asking a better way or the correct form to do it, but OK this is the script that in every part of the body in the enemy:
var damage = 5.0;
function CanDie(moredamage) {
var enemy = GameObject.FindWithTag("enemy").transform;
damage = damage + moredamage;
enemy.SendMessage("can_die",damage,SendMessageOptions.DontRequireReceiver);
}
for the bullet script is:
var damage = 2;
var damagereciver = true;
function OnCollisionEnter(collisionInfo) {
collisionInfo.other.SendMessage("canDie", damage,
SendMessageOptions.DontRequireReceiver);
}
What I do its that before this the bullet has a script that detects if the object it touches is has a tag named "part" and all the parts of the enemy have that tag and have a Collider to. Ok, the really problem are 3 things.
- The script dosent work to well(It dosent work when you are far and you shoot, only when its closer)
- Cause of the collider on every part of the enemy the enemy controls to bad
- Sometimes I dont know why it dosent get on collision(I dont know if is because my bullet goes really fast or something like that)
But, Please answer I will really appreciate the answers.
Answer by Loius · Dec 09, 2010 at 03:39 AM
This line is wrong:
var enemy = GameObject.FindWithTag("enemy").transform;
That will only find one object tagged enemy.
Here's how I do it -
Enemy.js -
var health : int; var parts : Array();
function Start() { parts = GetComponentsInChildren( EnemyPart ); for ( var p : EnemyPart in parts ) { p.enemy = this; } } function ApplyDamage( amount : int ) { health -= amount; if ( health <= 0 ) Die(); }
EnemyPart.js -
var enemy : Enemy;
function ApplyDamage( amount : int ) { enemy.ApplyDamage( amount ); }
Then you just apply Enemy to the enemy's root object, and EnemyPart to each damagable part. Call a part's ApplyDamage function and it'll send the message to the Enemy itself.
Remember that objects should only handle the information they need to know. Your foot (EnemyPart) doesn't tell your mind (Enemy) how much damage your body has taken; it only tells you when it is hurt. It's up to your mind to handle that information.
but I dont know, I try it and it works but I need to try this one that you put to know if it works better. But this is not finished, I almost have problems with the enemy that its moving to wired.
Oh know, thanks, yeah I was rong, I check the game and I see that when I kill the enemy it dosent kills that enemy, kills another one, again thanks.
Answer by Statement · Dec 09, 2010 at 12:44 AM
1: The script dosent work to well(It dosent work when you are far and you shoot, only when its closer)
Well, I don't see any script that is doing any shooting so I can't help you here, but perhaps it's linked with issue #3. Read on.
2: Cause of the collider on every part of the enemy the enemy controls to bad.
Sorry, this doesn't make any sense to me. Is this an animation issue?
3: Sometimes I dont know why it dosent get on collision(I dont know if is because my bullet goes really fast or something like that)
Yes, it is likely you're experiencing tunneling. Tunneling happens when the update intervals are too coarse and the object is moving too fast. I think you can try switching to continous physics, or change your bullet detection method to ray cast or sphere cast instead of moving entities around - which I presume you're doing now.
for the second one is that I have every part of the enemy(leg,head,foot, etc..) with a collider, but because of that the enemy starts moving to wired, but this dosent have to be with nothing of animation. And for the first one I already modify the question so please look it and please answer if you know.
dont worry I already find the answer of my question I only need to change collision to trigger but thanks anyway for your answer.
Your answer
Follow this Question
Related Questions
How can i make a projectile attack travel through multiple enemies dealing damage to all of them? 1 Answer
Best way to handle melle attacks in a fast paced 2D game 0 Answers
Object moving too fast, so that the collider does not work (C#) 2 Answers
Problem with melee collision 3 Answers
Attack Player if Enemy is hit 0 Answers