Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Health regen issue

Discussion in 'Scripting' started by Paykoman, Nov 15, 2014.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys i hv this script work properly with my health going up and down with damage i take but if i let my character low is health until 0 the regeneration dont let me die... i go to health = 0.1 and it regens 5 and so one in circle so i never get dead. Wt i hv to change here so if i get last hit i die?

    Appreciate all possible help

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Fighter : MonoBehaviour
    5. {
    6.     public float globeHeight;
    7.     public Texture globepicBack;
    8.     public Texture globepic;
    9.     public int globesize;
    10.     public float healthpercent;
    11.  
    12.     public GameObject opponent;
    13.     public CharacterController controller;
    14.    
    15.     public AnimationClip attack;
    16.     public AnimationClip dieClip;
    17.  
    18.     public float maxHealth;
    19.     public float health;
    20.     public int damage;
    21.  
    22.     public float regeneration;
    23.    
    24.     private double impactLenght;
    25.  
    26.     public double impactTime;
    27.     public bool impacted;
    28.     public bool inAction;
    29.    
    30.     public float range;
    31.    
    32.     bool started;
    33.     bool ended;
    34.    
    35.     public float combatEscapeTime;
    36.    
    37.     public float countDown;
    38.    
    39.     public bool specialAttack;
    40.    
    41.     // Use this for initialization
    42.     void Start ()
    43.     {
    44.         health = maxHealth;
    45.         impactLenght = (animation [attack.name].length * impactTime);
    46.     }
    47.    
    48.     // Update is called once per frame
    49.     void Update ()
    50.     {
    51.         if (Input.GetMouseButtonUp(1) && !specialAttack)
    52.         {
    53.             inAction = true;  
    54.         }
    55.  
    56.         if (Input.GetKey(KeyCode.Escape))
    57.         {
    58.             opponent = null;
    59.         }
    60.        
    61.        
    62.         if(inAction)
    63.         {
    64.             if(attackFunction (0, 1, KeyCode.Mouse1, null, 0, true))
    65.             {
    66.                
    67.             }
    68.             else
    69.             {
    70.                 inAction = false;
    71.             }
    72.         }
    73.  
    74.         if (health < maxHealth)
    75.         {
    76.             health += regeneration*Time.deltaTime;
    77.             if(health > maxHealth)
    78.             {
    79.                 health = maxHealth;
    80.             }
    81.         }
    82.        
    83.         die ();
    84.     }
    85.    
    86.     public bool attackFunction(int stunSeconds, double scaledDamage, KeyCode key, GameObject particleEffect, int projectile, bool opponentBase)
    87.     {
    88.         if (opponentBase)
    89.         {
    90.             if (Input.GetKey (key) && inRange ())
    91.             {
    92.                 animation.CrossFade (attack.name);
    93.                 ClickToMove.attack = true;
    94.                
    95.                 if (opponent != null)
    96.                 {
    97.                     transform.LookAt (opponent.transform.position);
    98.                 }
    99.             }
    100.         }
    101.         else
    102.         {
    103.             if (Input.GetKey (key))
    104.             {
    105.                 animation.CrossFade (attack.name);  
    106.                 ClickToMove.attack = true;
    107.                 transform.LookAt (ClickToMove.cursorPosition);
    108.             }
    109.         }
    110.        
    111.         if (animation[attack.name].time>0.9*animation[attack.name].length)
    112.         {
    113.             ClickToMove.attack = false;
    114.             impacted = false;
    115.             if(specialAttack)
    116.             {
    117.                 specialAttack = false;
    118.             }
    119.            
    120.             return false;
    121.         }
    122.        
    123.         impact(stunSeconds, scaledDamage, particleEffect, projectile, opponentBase);
    124.         return true;
    125.     }
    126.    
    127.     public void resetAttackFunction()
    128.     {
    129.         ClickToMove.attack = false;                                //stop imediatly the auto-attack if used a special attack
    130.         impacted = false;
    131.         animation.Stop (attack.name);
    132.     }
    133.    
    134.     void impact(int stunSeconds, double scaledDamage, GameObject particleEffect, int projectile, bool opponentBase)
    135.     {
    136.         if ((!opponentBase || opponent!= null) && animation.IsPlaying(attack.name)&&!impacted)
    137.         {
    138.             if((animation[attack.name].time)>impactLenght&&(animation[attack.name].time<0.9*animation[attack.name].length))
    139.             {
    140.                 countDown = combatEscapeTime - 2;
    141.                 CancelInvoke("combatEscapeCountDown");
    142.                 InvokeRepeating("combatEscapeCountDown", 0, 1);
    143.                 if(opponentBase)
    144.                 {
    145.                     opponent.GetComponent<Mob>().getHit(damage+scaledDamage);
    146.                     opponent.GetComponent<Mob>().getStun(stunSeconds);
    147.                 }
    148.                
    149.                 //Send out spheres
    150.                 Quaternion rot = transform.rotation;
    151.                 rot.x = 0;
    152.                 rot.y = 0;
    153.                
    154.                 if (projectile > 0)
    155.                 {
    156.                     Instantiate(Resources.Load("Projectile"), new Vector3(transform.position.x, transform.position.y+1.5f, transform.position.z), transform.rotation);
    157.                 }
    158.                
    159.                 //Play the particle effect
    160.                 if(particleEffect!=null)
    161.                 {
    162.                     Instantiate(particleEffect, new Vector3(opponent.transform.position.x, opponent.transform.position.y + 1.5f, opponent.transform.position.z), Quaternion.identity);
    163.                 }
    164.                
    165.                 impacted = true;
    166.             }
    167.         }
    168.     }
    169.    
    170.     void combatEscapeCountDown()
    171.     {
    172.         countDown = countDown - 1;
    173.         if (countDown == 0)
    174.         {
    175.             CancelInvoke("combatEscapeCountDown");
    176.         }
    177.     }
    178.    
    179.     public void getHit(int damage)
    180.     {
    181.         health = health - damage;
    182.  
    183.         if (health < 0)
    184.         {
    185.             health = 0;  
    186.         }
    187.     }
    188.    
    189.     bool inRange()
    190.     {
    191.         if (Vector3.Distance (opponent.transform.position, transform.position) <= range)
    192.         {
    193.             return true;  
    194.         }
    195.         else
    196.         {
    197.             return false;  
    198.         }
    199.     }
    200.    
    201.     //if dead returns true else returns false
    202.     public bool isDead()
    203.     {
    204.         if (health <= 0)
    205.         {
    206.             return true;
    207.         }
    208.         else
    209.         {
    210.             return false;
    211.         }
    212.     }
    213.    
    214.     void die()
    215.     {
    216.         if (isDead() && !ended)
    217.         {
    218.             if(!started)
    219.             {
    220.                 ClickToMove.die = true;
    221.                 animation.CrossFade(dieClip.name);
    222.                 started = true;
    223.             }
    224.            
    225.             if(started && !animation.IsPlaying(dieClip.name))
    226.             {
    227.                 //what ever u want to do
    228.                 Debug.Log ("You are dead!");
    229.                 health = 100;
    230.                
    231.                 ended = true;
    232.                 started = false;
    233.                 ClickToMove.die = false;
    234.             }
    235.         }
    236.     }
    237.  
    238.     void OnGUI()
    239.     {
    240.         healthpercent = health / maxHealth;
    241.  
    242.         if (healthpercent < 0) {
    243.             healthpercent = 0;  
    244.         }
    245.  
    246.         if (healthpercent > 1) {
    247.             healthpercent = 1;      
    248.         }
    249.  
    250.         GUI.BeginGroup (new Rect (20, Screen.height - (100 + 20), globesize, globesize));
    251.         GUI.DrawTexture (new Rect (0, -globesize + 100, globesize, globesize), globepicBack);
    252.         GUI.EndGroup ();
    253.  
    254.         globeHeight = healthpercent * globesize;
    255.         GUI.BeginGroup (new Rect (20, Screen.height - (globeHeight + 20), globesize, globesize));
    256.         GUI.DrawTexture (new Rect (0, -globesize + globeHeight, globesize, globesize), globepic);
    257.         GUI.EndGroup ();
    258.  
    259.  
    260.  
    261.     }
    262. }
     
  2. slay_mithos

    slay_mithos

    Joined:
    Nov 5, 2014
    Posts:
    130
    well, as it is set up, you will have trouble getting downed unless the killing blow happens when mouse 1 is down or during your special attack.

    You might want to setup a small timer after your attacks to that you don't start regen instantly.

    All of this, because the die() function (that you might want t rename Die, naming) is called after the regen of life in your Update.
    You could also put the function call before the health regen in your Update, it should at least work.
     
    Paykoman likes this.
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Y it works... I forget the orther of the calls... I know i start this at 1 week and im in learning process yet so i hv lots of things to fix start specially with navmesh and states... Tha'ts killing me :)

    Ty for ur help and tips