Update Health

How Would i add some code to this so when i get a upgrade/health pack?? I need the health to add points and the health bar to go up 1 length every time i get health

var maximumHitPoints = 100.0;
var hitPoints = 100.0;

var bulletGUI : GUIText;
var rocketGUI : DrawRockets;
var healthGUI : GUITexture;

var walkSounds : AudioClip[];
var painLittle : AudioClip;
var painBig : AudioClip;
var die : AudioClip;
var audioStepLength = 0.3;

private var machineGun : MachineGun;
private var rocketLauncher : RocketLauncher;
private var healthGUIWidth = 0.0;
private var gotHitTimer = -1.0;

var rocketTextures : Texture[];

function Awake () {
	machineGun = GetComponentInChildren(MachineGun);
	rocketLauncher = GetComponentInChildren(RocketLauncher);
	
	PlayStepSounds();

	healthGUIWidth = healthGUI.pixelInset.width;
}

function ApplyDamage (damage : float) {
	if (hitPoints < 0.0)
		return;

	// Apply damage
	hitPoints -= damage;

	// Play pain sound when getting hit - but don't play so often
	if (Time.time > gotHitTimer  painBig  painLittle) {
		// Play a big pain sound
		if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
			audio.PlayOneShot(painBig, 1.0 / audio.volume);
			gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
		} else {
			// Play a small pain sound
			audio.PlayOneShot(painLittle, 1.0 / audio.volume);
			gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
		}
	}

	// Are we dead?
	if (hitPoints < 0.0)
		Die();
}

function Die () {
	if (die)
		AudioSource.PlayClipAtPoint(die, transform.position);
	
	// Disable all script behaviours (Essentially deactivating player control)
	var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
	for (var b in coms) {
		var p : MonoBehaviour = b as MonoBehaviour;
		if (p)
			p.enabled = false;
	}

	LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);
}

function LateUpdate () {
	// Update gui every frame
	// We do this in late update to make sure machine guns etc. were already executed
	UpdateGUI();
}

function PlayStepSounds () {
	var controller : CharacterController = GetComponent(CharacterController);

	while (true) {
		if (controller.isGrounded  controller.velocity.magnitude > 0.3) {
			audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
			audio.Play();
			yield WaitForSeconds(audioStepLength);
		} else {
			yield;
		}
	}
}


function UpdateGUI () {
	// Update health gui
	// The health gui is rendered using a overlay texture which is scaled down based on health
	// - Calculate fraction of how much health we have left (0...1)
	var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);

	// - Adjust maximum pixel inset based on it
	healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;

	// Update machine gun gui
	// Machine gun gui is simply drawn with a bullet counter text
	if (machineGun) {
		bulletGUI.text = machineGun.GetBulletsLeft().ToString();
	}
	
	// Update rocket gui
	// This is changed from the tutorial PDF. You need to assign the 20 Rocket textures found in the GUI/Rockets folder
	// to the RocketTextures property.
	if (rocketLauncher)	{
		rocketGUI.UpdateRockets(rocketLauncher.ammoCount);
		/*if (rocketTextures.Length == 0) {
			Debug.LogError ("The tutorial was changed with Unity 2.0 - You need to assign the 20 Rocket textures found in the GUI/Rockets folder to the RocketTextures property.");
		} else {
			rocketGUI.texture = rocketTextures[rocketLauncher.ammoCount];
		}*/
	}
}

I only glanced over your code, but typically you would create a ‘health pack’ game object with a collider marked as a trigger, and then in the OnTriggerEnter() function for either your player or the ‘health pack’ object, if the player touches the health pack, destroy the health pack object and increase the player’s health.

yeah but what var do i update and what do i type to add health to cur health

The variable you want to update is ‘hitPoints’, and you would type something like:

hitPoints += hitPointsToAdd;

Thank You. I dont know if you would know but, how would i make the screen flash red if you were damaged? the screen fades to white when you die but that doesnt work for damage.

Whatever method you’re using for the fade-to-white should work for damage as well. (Obviously the details will be a bit different, but the concept will most likely be the same.)

Yeah but i’m using this,

 LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);

So, Idk what i would use to just have a quick flash, since it’s loading a level… what other " fades" are there?

Is ‘LevelLoadFade’ a script from the FPS tutorial?

In any case, take a look at the code, and see how they do it. I think there’s a good chance that a fullscreen rect is rendered using the GUI system, with alpha varying over time. For the effect you want, you’d do something similar, except you’d probably want to fade out rather than in (that is, the alpha will decrease over time rather than decrease).

You could probably create a prefab that simply renders a full-screen alpha-blended red rectangle with alpha decreasing from 1 to 0 over some short interval, and then removes itself. Then, spawn a game object from this prefab whenever you want the effect to occur.

Again though, take a look at the script to see how they do it. Then, if you have specific questions about how to create a similar effect, post back here.