[SOLVED]"PowerBar" descreasing on mouse pre

Ok, so i’m trying to make a script that reduces a bar when the mouse is being pressed i know i’m doing it wrong so please give me an advice here’s a code:

var maximumHitPoints = 10.0;
var hitPoints = 10.0;
var healthGUI : GUITexture;
var die : AudioClip;
var buttonDown : boolean = false;
//var resetPoints : float = 0.0;
var damage : float = 0.0;

private var healthGUIWidth = 0.0;

function Update() {
 if (buttonDown) 
 damage+=Time.deltaTime;
 }
function buttonPress(){
	if (Input.GetButtonDown("Fire1"))
	buttonDown = true;
}

function Awake () {

	healthGUIWidth = healthGUI.pixelInset.width;
}

function ApplyDamage () {
	hitPoints -= damage;
	if (hitPoints < 0.0)
		Die();
}

function Die () {
	if (die)
		AudioSource.PlayClipAtPoint(die, transform.position);
}

function LateUpdate () {
	UpdateGUI();
}

function UpdateGUI () {
	
	var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);

	
	healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;

}
var maximumHitPoints = 10.0;
var hitPoints = 10.0;
var healthGUI : GUITexture;
var die : AudioClip;
var buttonDown : boolean = false;
//var resetPoints : float = 0.0;
var damage : float = 0.0;

private var healthGUIWidth = 0.0;

function Awake () {
   healthGUIWidth = healthGUI.pixelInset.width;
}

function Update() {
 if (Input.GetButton("Fire1"))
 {
  damage = Time.deltaTime;
  ApplyDamage();
 }
}

function LateUpdate () {
   UpdateGUI();
}

function ApplyDamage () {
   hitPoints -= damage;
   if (hitPoints < 0.0)
      Die();
}

function Die () {
   if (die)
      AudioSource.PlayClipAtPoint(die, transform.position);
}

function UpdateGUI () {
   
   var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);

   
   healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;

}

how does this work? i didn’t test it

U’re my God and my saviour!
works greate!

as i see in inspector when i hold my mouse “Hit Points” are reducing constantly. Is there a way to limit it? For example if “Hit Points” goes to 0 reduction stops. And when i release the mouse the bar will grow slowly back to its initial position.

P.S. I’m sure that such script would be wery usable for the community.

function Update() {
 if (Input.GetButton("Fire1"))
 {
  damage = Time.deltaTime;
  ApplyDamage();
 }
 else
 {
   hitPoints += Time.deltaTime;
 }
 Mathf.Clamp(hitPoints, 0, maximumHitPoints);
}

That’s the point! But still the same problem when mouse is pressed “Hit Points” are reducing constantly. and when mouse is released - “Hit Points” are increasing constantly.

oops, change

Mathf.Clamp(hitPoints, 0, maximumHitPoints);

to

hitPoints = Mathf.Clamp(hitPoints, 0, maximumHitPoints);

U got it right everything works perfectly all i can add is just:

else
 {
   hitPoints += Time.deltaTime*0.2;

to make bar slowly increasing.
Thank you again dbp!

Ok last question. I promise!
So i got a standart drag rigidBody script so i’m trying to modify it so if “hitPoints” < 1, player is unable to drag a rigid body and if “hitPoints” > 1 dragging is on.
As far as i know i’ve got to change var hitPoints = 0.0;
to static variable and pass it to DragRigidBody script. But how to stop dragging function???

Ok. So here’s what i’ve done:
changed variable

var hitPoints = 10.0;

to static

static var hitPoints : float = 0;

in a “PowerBar” which u can see above.
and added this string to “DragRigidBody” script

function Update ()
{
if (PowerBar.hitPoints < 1)
spring = 0;
else if (PowerBar.hitPoints > 1)
spring = 50;

But UNITY says BCE0005: Unknown identifier: 'PowerBar'.
What i’m doing wrong??

Solution:

function Update ()
{
if (GameObject.Find ("powerBar").GetComponent("PowerBar").hitPoints  < 1)
spring = 0;
else if (GameObject.Find ("powerBar").GetComponent("PowerBar").hitPoints > 1)
spring = 50;

Wrong, (“PowerBar”) to (PowerBar)

(“PowerBar”) works just perfect…