x


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?

more ▼

asked Jan 10 '10 at 03:12 AM

Babilinski gravatar image

Babilinski
84 40 42 43

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

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.

more ▼

answered Jan 10 '10 at 03:57 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5081
x823

asked: Jan 10 '10 at 03:12 AM

Seen: 1021 times

Last Updated: Jan 10 '10 at 03:12 AM