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