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

Need help with damaging enemy.

Discussion in 'Scripting' started by ZGMGames, Nov 29, 2016.

  1. ZGMGames

    ZGMGames

    Joined:
    Sep 30, 2016
    Posts:
    77
    I'm using simple projectile as my bullet. I'm now trying to use the weapons damage as the method to reduce enemy health. Here's my starter script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Zombie : MonoBehaviour {
    6.  
    7.     Collider other;
    8.     WeaponStats weapon;
    9.     GameObject bullet;
    10.     Animator anim;
    11.     public int enemyHealth;
    12.     bool playerinrange;
    13.  
    14.     void Start ()
    15.     {
    16.         weapon = GetComponent<WeaponStats> ();
    17.         other = GetComponent<Collider> ();
    18.         anim = GetComponent<Animator> ();
    19.         bullet = GameObject.Find ("bullet");
    20.     }
    21.  
    22.  
    23.     void Update()
    24.     {
    25.         Attack ();
    26.         TakeDamage ();
    27.     }
    28.    
    29.     public void Attack()
    30.     {
    31.        
    32.     }
    33.  
    34.     public void TakeDamage()
    35.     {
    36.         if (other.tag == "bullet")
    37.         {
    38.             enemyHealth = enemyHealth - weapon.damage;
    39.         }
    40.  
    41.         if (enemyHealth < 1)
    42.         {
    43.             DestroyObject (this.gameObject);
    44.         }
    45.     }
    46. }
    47.  
    48.  
    My weapon is setup using inheritance. Weapon Stats > Shot type > Weapon


    I cannot get the enemy to reduce health when the two colliders meet.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. ZGMGames

    ZGMGames

    Joined:
    Sep 30, 2016
    Posts:
    77
    Code (csharp):
    1.  
    2. public void OnCollisionEnter(Collider other)
    3.     {
    4.         if (other.tag == "bullet")
    5.         {
    6.             enemyHealth -= weapon.damage;
    7.             Destroy (bullet);
    8.         }
    9.    
    10.         if (enemyHealth < 1)
    11.         {
    12.             DestroyObject (this.gameObject);
    13.         }
    14.     }
    15. }
    16.  
    still no luck.
     
  4. ZGMGames

    ZGMGames

    Joined:
    Sep 30, 2016
    Posts:
    77
    OnTriggerEnter worked..
     
  5. magnite

    magnite

    Joined:
    Dec 12, 2012
    Posts:
    125
    Do both objects have collider? Is on collider actually a trigger? How fast is the bullet moving? If it is moving too fast then it may fly past the enemy in a single frame, resulting in the collision to be not caught.

    If OnTriggerEnter worked then one collider is a trigger not a collider.