- Home /
The question is answered, right answer was accepted
each enemy have different damage amount, how to calculate the hp remain???
so lets say there are different enemy
each enemy attach a script with damage variable
than player trigger by the collide of enemy to access the damage component?Is is workable? is there better method??
collider enermy1;
damage = enermy1.GetComponent<damagescript>().damagevariable;
hp = hp-damage;
im worrying that since my game target mobile platform if there is many enemy around, "GetComponent" would cause performance issue...
HELLO dear OP? Please tick an answer to keep the board tidy and clean. If an answer wasn't helpful to you, proper feedback would be appreciated. Thanks.
Answer by vexe · Aug 14, 2013 at 07:19 AM
You can approach it the other way around. You say there are dif enemies, each have dif damages amounts. But there is one player, right? ;) - Let all your types of enemies have a reference to your player. Set the ref at your enemy Start:
Player player;
void Start() // or function Start() on JS
{
player = GameObject.FindWithTag("Player");
// ... rest of your stuff
}
That way, you don't have to take the damage variable from your enemy, teach time they hit you. Instead, you do the opposite:
if (collider is an enemy)
player.InflictDamage(damage);
And let the player decide how he should take damage:
void InflictDamage(int damage)
if (hp > 0)
hp -= damage;
You can also have a game controller, that controls your whole game. It could act as an interface of communication between all your objects, enemies, players, etc. So when your enemy hits you, you can notify the gameController that your player has taken damage, and it will take the necessary measure
gameController.Notify_PlayerHit(enemyDamageAmount);
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
MiniMap Icons 0 Answers
How do i make an animation play on key press? 3 Answers
Getting Error Object reference not set to an instance of an object 1 Answer
Trap Door Question 1 Answer