is there a way i can make it so when my player attacks a enemy it shows the enemies health
Sure.
Access his health from a script. Then display his health.
how do i do that
Many possible solutions. I won’t do any scripting for yourself, but here is a possibility.
When you select your target, you can get the character’s traits from something along the line of :
target : Enemy = SelectTarget();
if (targetedEnemy) {
displayEnemyHealth : bool = true;
}
///
function OnGUI() {
if (displayEnemyHealth) {
// run code
}
}
function SelectTarget() { }
Note, this is just psuedo-code, to show you how one would go about this. If you can’t figure out how to do it from here – stop whatever you’re dong, and learn how to program.
- Alex
Ya, little harsh there gamesurgeon.
If you don’t understand how to do this, I recommend reading some of the unity3d tutorials. Once done, check the scripting reference.
To get an enemies health you should check getcomponent on the scripting reference.
:? Not being harsh! Just being succinct. If the person doesn’t know how to do something as basic as access a variable from another script, he/she needs to learn to program! I guess you’re just nicer than me ![]()
I didn’t know how till last week! It all depends on what you are actually scripting. ![]()
There are two typical ways I do this.
-
If it’s a player or something of the sort – we only want to be able to access things we target. Therefore, we can just put a collider on it, and shoot a raycast from the camera to the mouse position. An example would be, targeting an enemy. Say, there’s a script attached to the collider, Enemy. My code above works for that.
-
If we’re not targeting the enemy, say it’s AI that wants to attack a human. What we can do is use a mixture of FindWithTag(“MainPlayerOrWhatever”). Then use GetComponent();
I don’t know if there’s other ways, but those are pretty easy to use and setup. Especially the the one with the collider. That’s my favorite :lol: