Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Player Can't Die If Attacking, And Has God Mode Upon Initial Death

Discussion in 'Scripting' started by caleblich, Apr 14, 2019.

  1. caleblich

    caleblich

    Joined:
    Apr 18, 2016
    Posts:
    18
    For some reason with my script, when my character dies, he has god mode and cannot die again. Also, if my character is attacking and my health hits 0 whilst he's attacking, he does not die and cannot die. I'll attach the player script which includes his health and combat, and also if needed I can attach the enemy script too.

    Any help is appreciated thanks!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class combat : MonoBehaviour
    5. {
    6.     public GameObject opponent;
    7.  
    8.     public AnimationClip attack;
    9.     public AnimationClip dieClip;
    10.     public float range;
    11.     public int damage;
    12.     public int health;
    13.     public int maxHealth;
    14.     private double impactLength;
    15.  
    16.     public double impactTime;
    17.  
    18.     public bool impacted;
    19.  
    20.     bool started;
    21.     bool ended;
    22.  
    23.     public float combatEscapeTime;
    24.  
    25.     public float countDown;
    26.  
    27.  
    28.     // Use this for initialization
    29.     void Start ()
    30.     {
    31.         impactLength = (GetComponent<Animation>()[attack.name].length*impactTime);
    32.     }
    33.  
    34.  
    35.     // Update is called once per frame
    36.     void Update ()
    37.     {
    38.     Debug.Log (health);
    39.         if(Input.GetKey(KeyCode.Space)&&inRange ())
    40.         {
    41.             GetComponent<Animation>().Play(attack.name);
    42.             ClickToMove.attack = true;
    43.          
    44.             if(opponent!=null)
    45.             {
    46.                 transform.LookAt(opponent.transform.position);
    47.              
    48.             }
    49.         }
    50.      
    51.         if(GetComponent<Animation>()[attack.name].time<0.9*GetComponent<Animation>()[attack.name].length)
    52.         {
    53.             ClickToMove.attack = false;
    54.             impacted = false;
    55.         }
    56.      
    57.         impact ();
    58.         die ();
    59.     }
    60.  
    61.     void impact()
    62.     {
    63.         if(opponent!=null&&GetComponent<Animation>().IsPlaying (attack.name)&&!impacted)
    64.         {
    65.             if((GetComponent<Animation>()[attack.name].time)>impactLength&&(GetComponent<Animation>()[attack.name].time<0.9*GetComponent<Animation>()[attack.name].length))
    66.             {
    67.                 countDown = combatEscapeTime + 2;
    68.                 CancelInvoke ();
    69.                 InvokeRepeating ("combatEscapeCountDown", 0, 1);
    70.                 opponent.GetComponent<mob>().GetHit(damage);
    71.                 impacted = true;
    72.              
    73.             }
    74.         }
    75.     }
    76.  
    77.     void combatEscapeCountDown()
    78.     {
    79.       countDown = countDown - 1;
    80.       if(countDown == 0)
    81.       {
    82.             CancelInvoke("combatEscapeCountDown");
    83.       }
    84.     }
    85.  
    86.     public void GetHit(int damage)
    87.     {
    88.      health = health - damage;
    89.       if (health<0)
    90.       {
    91.         health = 0;
    92.       }
    93.     }
    94.     bool inRange()
    95.     {
    96.      
    97.         if(Vector3.Distance (opponent.transform.position, transform.position) <= range)
    98.         {
    99.             return true;
    100.         }
    101.      
    102.         else
    103.         {
    104.             return false;
    105.         }
    106.      
    107.      
    108.     }
    109.  
    110.     //if dead returns true or alive turns false
    111.     public bool isDead()
    112.     {
    113.       if(health == 0)
    114.       {
    115.         return true;
    116.       }
    117.    
    118.       else
    119.       {
    120.         return false;
    121.       }
    122.     }
    123.  
    124.  
    125.     void die()
    126.     {
    127.           if(isDead ()&&!ended)
    128.           {
    129.             if(!started)
    130.             {
    131.             ClickToMove.die = true;
    132.             GetComponent<Animation>().Play (dieClip.name);
    133.             started = true;
    134.             }
    135.          
    136.             if(started&&!GetComponent<Animation>().IsPlaying(dieClip.name))
    137.             {
    138.             //what ever you want
    139.              
    140.            
    141.              maxHealth = 1000;
    142.              
    143.                 ended = true;
    144.                 started = false;
    145.                 ClickToMove.die = false;
    146.              
    147.             }
    148.        
    149.           }
    150.        
    151.     }
    152. }
    153.  
     
    Last edited: Apr 14, 2019
  2. caleblich

    caleblich

    Joined:
    Apr 18, 2016
    Posts:
    18
    any one able to help?
     
  3. Yulgorath

    Yulgorath

    Joined:
    Mar 30, 2019
    Posts:
    1