Monday, 11 March 2013

HealthControl Script

using UnityEngine;using System.Collections;

public class HealthControl : MonoBehaviour
{   public TextMesh tmPlayerHealth; 

   public int playerHealth = 0;
   private float updateDisplayTime = 0F;
 
// Use this for initialization
void Start ()
 {
 
   playerHealth = 100;
   DisplayHealth();
 updateDisplayTime = Time.time + 5F;

}
void OnDisable()

{

   Destroy( gameObject );

}
// Update is called once per frame
void Update ()
{
   if ( updateDisplayTime < Time.time )
   {

        playerHealth -= 10;

        DisplayHealth();

        updateDisplayTime = Time.time + 5F;

   }
   if ( playerHealth <= 0 )
   {
       this.enabled = false;
   }


}
void OnControllerColliderHit( ControllerColliderHit collisionObj )

{   if ( collisionObj.gameObject.tag == "Enemy" )
   {

        playerHealth--;

        DisplayHealth();

   }



        Debug.Log ("OnControllerColliderHit detected with tag "+collisionObj.gameObject.tag);
 
} // This method is not called when using a CharacterController object
// Use OnControllerColliderHit() method instead

void OnCollisionEnter( Collision collisionObj )
{
    if ( collisionObj.gameObject.tag == "Enemy" )
    {

         playerHealth--;

         DisplayHealth();
    }


Debug.Log (
"OnCollisionEnter detected with tag "+collisionObj.gameObject.tag);

}void OnTriggerStay( Collider colliderObj )
{
    if ( colliderObj.gameObject.tag == "Friend" )
    {

        playerHealth++;

        DisplayHealth();

    }

}
void DisplayHealth()

  {

     tmPlayerHealth.text = playerHealth.ToString();

  }

}


This is the script to control the health when you enter the methane field.

No comments:

Post a Comment