Search Unity

Trying to make enemy lose hp when hit. (Beginner)

Discussion in 'Scripting' started by Deleted User, Apr 21, 2015.

  1. Deleted User

    Deleted User

    Guest

    I've got no errors, so it's just something I'm missing. The enemy gets destroyed if I set it to 0 so that works but when ever I hit it with the hammer it just doesn't lose any hp. Explain it simply if you can. I'm trying my best to learn so every kind of feedback would be very useful :) I feel like it something to do with the currentHealth -= 2;
    Thanks
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CombatScript : MonoBehaviour
    5. {  
    6.     public GameObject hammer;
    7.     public int currentHealth = 5;
    8.     public int enemyHealth = 6;
    9.  
    10.     void update()
    11.     {
    12.         hammer = GameObject.FindGameObjectWithTag("Character_ThorModel_Hammer");
    13.     }
    14.     public void OnTriggerEnter(Collider other)
    15.     {
    16.          
    17.         if(enemyHealth <= 0)
    18.             {
    19.               Destroy (gameObject);
    20.             }
    21.         }
    22.  
    23.     void OnCollisionEnter (Collision col)
    24.     {
    25.         {
    26.             if(col.gameObject.name == "Character_ThorModel_hammer")
    27.             {
    28.                 currentHealth -= 2;
    29.                 print ("Hit");
    30.             }
    31.         }
    32.     }
    33.     void adjustHealth()
    34.     {
    35.         if(currentHealth < 0)
    36.         {
    37.             currentHealth = 0;
    38.  
    39.         if(currentHealth > enemyHealth)
    40.             {
    41.  
    42.             currentHealth = enemyHealth;
    43.             }
    44.         }
    45.     }
    46. }
     
    Last edited by a moderator: Apr 21, 2015
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    Its better to have the hammer to the damage. If the hammer colides with something on the "character" tag, for instance, you can do something like...
    Code (csharp):
    1.  col.GetComponent<CombatScript>().DoDamage(2);
    You'll need a method that you can pass a damage variable through like...
    Code (csharp):
    1.  
    2. public void DoDamage(int incomingDamage)
    3. { health -= incomingDamage; }
    I would also recommend using a Property for your Health, so you can take care of it more effectively in the long run.
    https://unity3d.com/learn/tutorials/modules/intermediate/scripting/properties
     
    Deleted User likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    anything involving collisions requires something in the collision to have a rigidbody (no rigidbody, no physics engine, no collision)

    It's a little odd to see Trigger and Collision code together, are you colliding with a trigger collider or a non trigger collider? (i.e. what type of collider is on the hammer?)

    adjustHealth() never gets called... ? (also should really be AdjustHealth() functions are customarily capitalised)
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I think one of the "key" things about writing code is making things small and have each bit handle it's own "thing" and only that.

    So having 1 script to handle "health", it just keeps an eye on the number, has functions to be called when something else wants to change the health, checks for death etc. This script can then be put on anything that needs a health stat.

    Then having 1 script to handle "being a weapon", this handles just the damage, checks for hitting things, if that thing has a "health" script on it, tell the health script that damage was done using the functions on that health script. That way any object can have it added and it's now a weapon.