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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How Do I Make My Enemy Hit And Not Drain My Players Health (Please Help Fast!)

Discussion in 'Scripting' started by Darkdragon0011, May 4, 2015.

  1. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    Here is the code it works but i want 2 to 3 seconds where the enemy can't attack my player after i get hit
    Problem Located At The Last Four Lines.

    Code (JavaScript):
    1. #pragma strict
    2. var curHp : float = 100.0;
    3. var maxHp : int = 100;
    4. var currentThirst : float = 100.0;
    5. var maxThirst : int = 100;
    6. var currentHunger : float = 100.0;
    7. var maxHunger : int = 100;
    8. var lastPositionY : float = 0f;
    9. var fallDistance : float = 0f;
    10. var player : Transform;
    11. private var controller : CharacterController;
    12. private var barLength = 0.0;
    13. function Start()
    14. {
    15.     barLength = Screen.width / 8;
    16. }
    17. function Update()
    18. {
    19.     if(curHp <= 0)
    20.     {
    21.         CharacterDeath();
    22.     }
    23.  
    24.     /* THIRST CONTROL SECTION*/
    25.  
    26.     //Normal thirst degredation
    27.     if(currentThirst >= 0)
    28.     {
    29.         currentThirst -= Time.deltaTime / 5;
    30.     }
    31.  
    32.     if(currentThirst <= 0)
    33.     {
    34.         currentThirst = 0;
    35.     }
    36.  
    37.     if(currentThirst >= maxThirst)
    38.     {
    39.         currentThirst = maxThirst;
    40.     }
    41.  
    42.     /* HUNGER CONTROL SECTION*/
    43.         if(currentHunger >= 0)
    44.     {
    45.         currentHunger -= Time.deltaTime / 8;
    46.     }
    47.  
    48.     if(currentHunger <= 0)
    49.     {
    50.         currentHunger = 0;
    51.     }
    52.  
    53.     if(currentHunger >= maxHp)
    54.     {
    55.         curHp = maxHunger;
    56.     }
    57.  
    58.     /* DAMAGE CONTROL SECTION*/
    59.     if(currentHunger <= 0 && (currentThirst <= 0))
    60.     {
    61.         curHp -= Time.deltaTime / 4;
    62.         if (currentHunger <= 0){
    63.    
    64.             print ("You Died"); }
    65.     }
    66.  
    67.     else
    68.     {
    69.         if(currentHunger <= 0 || currentThirst <= 0)
    70.         {
    71.             curHp -= Time.deltaTime / 8;
    72.         }
    73.     }
    74. }
    75. function CharacterDeath()
    76. {
    77.     Application.LoadLevel("SimpleMenu");
    78. }
    79. function OnGUI()
    80. {
    81.     //Icons
    82.     GUI.Box(new Rect(5, 30, 50, 23), "Health");
    83.     GUI.Box(new Rect(5, 55, 50, 23), "Thirst");
    84.     GUI.Box(new Rect(5, 80, 50, 23), "Hunger");
    85.  
    86.     //Health / Hunger / Thirst bars
    87.     GUI.Box(new Rect(55, 30, barLength, 23), curHp.ToString("0") + "/" + maxHp);
    88.     GUI.Box(new Rect(55, 55, barLength, 23), currentThirst.ToString("0") + "/" + maxThirst);
    89.     GUI.Box(new Rect(55, 80, barLength, 23), currentHunger.ToString("0") + "/" + maxHunger);
    90.  
    91. }
    92. function OnTriggerEnter (other : Collider) {
    93.     if(other.gameObject == ("CocconutCol") && currentHunger <= 90.0){
    94.  
    95.         currentHunger += 10;
    96.         Destroy (other.gameObject.transform.parent.gameObject);
    97. }
    98. }
    99. function OnControllerColliderHit(hit : ControllerColliderHit){
    100.      if (hit.gameObject.CompareTag("Enemy")){
    101.  
    102.          curHp = curHp - 5;
    103.      }
    104. }
     
    Last edited: May 4, 2015
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Stop asking for "help fast". It's annoying.

    Clearly state what the problem is (ie - what is not working).
     
  3. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    So you know where the problem is? What is your question anyway? You don't know how to write the code?
     
  4. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    well i don't really know how to make 2 or 3 seconds where the enemy can't attack
     
  5. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    it works but the enemy just drains my health