- Home /
How to make an object and a value to a variable
in this case a health pack, my main player has the variable var hitPoints = 100.0 and when an object takes the health down what script would i use to bring them back up?
Answer by Jaap Kreijkamp · Jan 10, 2010 at 03:57 AM
Your player controller code could contain something like this:
var hitPoints : float = 100.0;
function ApplyDamage(dmg : float) { hitPoints = Mathf.Max(0.0, hitPoints - dmg); if (hitPoints <= 0.0) { // handle dying... } }
function Heal(points : float) { hitPoints = Mathf.Min(100.0, hitPoints + points); }
// rest of player controller code...
Make a health package have a collider with isTrigger checked and a script like:
function OnTriggerEnter(other : Collider) {
if (other.gameObject.tag == "Player") {
// tell player to heal
other.gameObject.SendMessage("Heal", 10.0, SendMessageOptions.DontRequireReceiver);
// destroy health package
GameObject.Destroy(gameObject);
}
}
About the same script can be used for objects applying damage (bullets, traps, ...) only Heal
should be replaced with ApplyDamage
.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Changing variables in GUI? 0 Answers
Gun Script Help 2 Answers
How to use Enum? 1 Answer
Array Problem - Error Code BCE0022 2 Answers