Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Applying Damage

Discussion in 'Scripting' started by BrokenhearT, May 29, 2015.

  1. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Hello everyone!
    Hope everything is going well, after I finished the script and it's working very well I realized that once I shoot the enemy some of them won't take damage while others do and also they won't be explode after I take their health down to <= 0
    and here's the scripts this is for enemy

    using UnityEngine;
    using System.Collections;

    public class Enemy : MonoBehaviour {

    public Transform Player;
    public float moveSpeed = 5;
    public float rotateSpeed = 10;
    public float mDistance = 10;

    public GameObject Bullet;
    public Transform BulletPoint;
    public float restTime;

    public float rTime;
    public int health = 100;
    public GameObject Explo;

    void Start ()
    {
    Player = GameObject.Find ("Player").transform;
    }

    void Update ()
    {
    if (Vector3.Distance (transform.position, Player.position) > mDistance)
    {
    transform.position += (Player.position - transform.position).normalized * moveSpeed * Time.deltaTime;

    if (health <= 0)
    {
    Instantiate (Explo, transform.position, Quaternion.identity);
    Destroy(gameObject);
    }
    }

    Shoot ();

    }

    void Shoot ()
    {
    if ( rTime >=0)
    rTime -= Time.deltaTime;

    if (rTime <= 0) {
    Instantiate (Bullet, BulletPoint.position, BulletPoint.rotation);
    rTime = restTime;
    }
    }

    void ApplyDamage ()
    {
    health -= 25;
    }
    }

    and here's the Bullet's script

    using UnityEngine;
    using System.Collections;

    public class Bullet : MonoBehaviour {

    public int speed = 10;

    public GameObject Expl;

    void Start () {

    }


    void Update ()
    {
    transform.position += transform.TransformDirection (new Vector3 (0, speed * Time.deltaTime, 0));
    Destroy (gameObject, 2.5f);
    }

    void OnCollisionEnter(Collision Others)
    {
    Instantiate (Expl, transform.position, Quaternion.identity);
    Others.gameObject.SendMessage ("ApplyDamage",SendMessageOptions.DontRequireReceiver );
    Destroy (gameObject);
    }
    }


    Thanks in advance!
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Please put your scripts in between CODE tags...
     
    SnakeTheRipper likes this.
  3. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    remember that for SendMessage the 2 scripts must be attached to the same gameObject
     
  4. BrokenhearT

    BrokenhearT

    Joined:
    May 18, 2015
    Posts:
    69
    Ok will do that now, and Yes Chubbspet they both are attached to same object.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy : MonoBehaviour {
    5.  
    6.     public Transform Player;
    7.     public float moveSpeed = 5;
    8.     public float rotateSpeed = 10;
    9.     public float mDistance = 10;
    10.  
    11.     public GameObject Bullet;
    12.     public Transform BulletPoint;
    13.     public float restTime;
    14.    
    15.     public float rTime;
    16.     public int health = 100;
    17.     public GameObject Explo;
    18.  
    19.     void Start ()
    20.     {
    21.         Player = GameObject.Find ("Player").transform;
    22.     }
    23.    
    24.     void Update ()
    25.     {
    26.         if (Vector3.Distance (transform.position, Player.position) > mDistance)
    27.         {
    28.             transform.position += (Player.position - transform.position).normalized * moveSpeed * Time.deltaTime;
    29.             transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Player.position - transform.rotation), rotateSpeed * Time.deltaTime);
    30.  
    31.             if (health <= 0)
    32.             {
    33.                 Instantiate (Explo, transform.position, Quaternion.identity);
    34.                 Destroy(gameObject);
    35.             }
    36.         }
    37.  
    38.         Shoot ();
    39.  
    40.     }
    41.  
    42.     void Shoot ()
    43.     {
    44.         if ( rTime >=0)
    45.             rTime -= Time.deltaTime;
    46.    
    47.         if (rTime <= 0) {
    48.             Instantiate (Bullet, BulletPoint.position, BulletPoint.rotation);
    49.             rTime = restTime;
    50.         }
    51.     }
    52.  
    53.     void ApplyDamage ()
    54.     {
    55.         health -= 25;
    56.     }
    57. }
    58.  










    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bullet : MonoBehaviour {
    5.    
    6.     public int speed = 15;
    7.    
    8.     public GameObject Expl;
    9.    
    10.     void Start () {
    11.        
    12.     }
    13.    
    14.    
    15.     void Update ()
    16.     {
    17.         transform.position += transform.TransformDirection(new Vector3 (0, 0, speed * Time.deltaTime));
    18.         Destroy (gameObject, 3f);
    19.     }
    20.    
    21.     void OnCollisionEnter(Collision Others)
    22.     {
    23.         Instantiate (Expl, transform.position, Quaternion.identity);
    24.         if (Others.gameObject.tag == "Player")
    25.             Others.gameObject.SendMessage ("ApplyDamage",5, SendMessageOptions.DontRequireReceiver);
    26.         else
    27.             Others.gameObject.SendMessage ("ApplyDamage",10, SendMessageOptions.DontRequireReceiver);
    28.         Destroy (gameObject);
    29.     }
    30. }
    31.