Search Unity

Adding red haze around camera when health is low

Discussion in 'Scripting' started by Teejay5, Mar 26, 2010.

  1. Teejay5

    Teejay5

    Joined:
    Feb 26, 2010
    Posts:
    101
    So, I have tried all day, but couldnt get this to work.
    So what I want to do is, when the player has only 40 hitpoints left (which would deem them severely hurt) I want a GUI image to be shown. but if the player gets a healthpack, I want the image to be hidden. I have edited a script from the FPS tutorial to do this, but it DOES NOT WOOORK!the parts that I edited have HERE written next to them.

    Code (csharp):
    1. var maximumHitPoints = 100.0;
    2. var hitPoints = 100.0;
    3.  
    4. var bulletGUI : GUIText;
    5. var rocketGUI : DrawRockets;
    6. var healthGUI : GUITexture;
    7.  
    8. var walkSounds : AudioClip[];
    9. var painLittle : AudioClip;
    10. var painBig : AudioClip;
    11. var die : AudioClip;
    12. var audioStepLength = 0.3;
    13.  
    14. private var machineGun : MachineGun;
    15. private var rocketLauncher : RocketLauncher;
    16. private var healthGUIWidth = 0.0;
    17. private var gotHitTimer = -1.0;
    18.  
    19. var rocketTextures : Texture[];
    20. var iconTexture : Texture[]; HERE
    21.  
    22. function Awake () {
    23.     machineGun = GetComponentInChildren(MachineGun);
    24.     rocketLauncher = GetComponentInChildren(RocketLauncher);
    25.    
    26.     PlayStepSounds();
    27.  
    28.     healthGUIWidth = healthGUI.pixelInset.width;
    29. }
    30.  
    31. function Start () {
    32.    iconTexture.enabled = false;
    33. }
    34.  
    35. function ApplyDamage (damage : float) {
    36.     if (hitPoints < 0.0)
    37.         return;
    38.  
    39.     // Apply damage
    40.     hitPoints -= damage;
    41.  
    42.     // Play pain sound when getting hit - but don't play so often
    43.     if (Time.time > gotHitTimer  painBig  painLittle) {
    44.         // Play a big pain sound
    45.         if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
    46.             audio.PlayOneShot(painBig, 1.0 / audio.volume);
    47.             gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
    48.         } else {
    49.             // Play a small pain sound
    50.             audio.PlayOneShot(painLittle, 1.0 / audio.volume);
    51.             gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
    52.         }
    53.     }
    54.     if (hitPoints < 40.0) HERE
    55.       iconTexture.enabled = true; HERE
    56.     }
    57.     // Are we dead?
    58.     if (hitPoints < 0.0) {
    59.         Die();
    60.     }
    61.  
    62. function Die () {
    63.     if (die)
    64.         AudioSource.PlayClipAtPoint(die, transform.position);
    65.    
    66.     // Disable all script behaviours (Essentially deactivating player control)
    67.     var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
    68.     for (var b in coms) {
    69.         var p : MonoBehaviour = b as MonoBehaviour;
    70.         if (p)
    71.             p.enabled = false;
    72.     }
    73.  
    74.     LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.black, 2.0);
    75. }
    76.  
    77. function LateUpdate () {
    78.     // Update gui every frame
    79.     // We do this in late update to make sure machine guns etc. were already executed
    80.     UpdateGUI();
    81. }
    82.  
    83. function PlayStepSounds () {
    84.     var controller : CharacterController = GetComponent(CharacterController);
    85.  
    86.     while (true) {
    87.         if (controller.isGrounded  controller.velocity.magnitude > 0.3) {
    88.             audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
    89.             audio.Play();
    90.             yield WaitForSeconds(audioStepLength);
    91.         } else {
    92.             yield;
    93.         }
    94.     }
    95. }
    96.  
    97.  
    98. function UpdateGUI () {
    99.     // Update health gui
    100.     // The health gui is rendered using a overlay texture which is scaled down based on health
    101.     // - Calculate fraction of how much health we have left (0...1)
    102.     var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
    103.  
    104.     // - Adjust maximum pixel inset based on it
    105.     healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
    106.  
    107.     // Update machine gun gui
    108.     // Machine gun gui is simply drawn with a bullet counter text
    109.     if (machineGun) {
    110.         bulletGUI.text = machineGun.GetBulletsLeft().ToString();
    111.     }
    112.    
    113.     // Update rocket gui
    114.     // This is changed from the tutorial PDF. You need to assign the 20 Rocket textures found in the GUI/Rockets folder
    115.     // to the RocketTextures property.
    116.     if (rocketLauncher) {
    117.         rocketGUI.UpdateRockets(rocketLauncher.ammoCount);
    118.         /*if (rocketTextures.Length == 0) {
    119.             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.");
    120.         } else {
    121.             rocketGUI.texture = rocketTextures[rocketLauncher.ammoCount];
    122.         }*/
    123.     }
    124. }


    OR, if anyone else has already prepared a script similar to this, please send it to me.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,208
    You have iconTexture defined as an array of textures, but you're trying to access it as if it was just one texture.

    --Eric
     
  3. Teejay5

    Teejay5

    Joined:
    Feb 26, 2010
    Posts:
    101
    Thanks for the quick reply!
    so how about this?:

    Code (csharp):
    1. var iconTexture : GUITexture;
    and

    Code (csharp):
    1.     if (hitPoints < 40.0)
    2.       iconTexture.enabled = true;
    3.     }
     
  4. Teejay5

    Teejay5

    Joined:
    Feb 26, 2010
    Posts:
    101
    Its working now! thank you.
    But one problem - My character cannot die...his health gets to 0 and stops.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,208
    That's because you say "if (hitPoints < 0.0) return", so none of the other code will ever run if the hitPoints are below 0.

    --Eric
     
unityunity