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. Dismiss Notice

Grenade damage to enemy problem

Discussion in 'Scripting' started by i3artyy2222, Jun 6, 2014.

  1. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hello,
    I have a problem because i have script like this for my grenade
    Code (JavaScript):
    1. var radius : float = 5.0;    //provides a radius at which the explosive will effect rigidbodies
    2. var power : float = 10.0;    //provides explosive power
    3. var explosiveLift : float = 1.0; //determines how the explosion reacts. A higher value means rigidbodies will fly upward
    4. var explosiveDelay : float = 5.0; //adds a delay in seconds to our explosive object
    5. var explosionPrefab : Transform;
    6. var explosionSound : AudioClip;
    7. function Update () {
    8. Fire();
    9. }
    10. function Fire () {
    11.   yield WaitForSeconds(explosiveDelay);
    12.   Instantiate(explosionPrefab, transform.position, transform.rotation);
    13.   var grenadeOrigin : Vector3 = transform.position;
    14.   var colliders : Collider[] = Physics.OverlapSphere (grenadeOrigin, radius); //this is saying that if any collider within the radius of our object will feel the explosion
    15. for(var hit : Collider in colliders){  //for loop that says if we hit any colliders, then do the following below
    16.    if (hit.rigidbody){
    17.       hit.rigidbody.AddExplosionForce(power, grenadeOrigin, radius, explosiveLift); //if we hit any rigidbodies then add force based off our power, the position of the explosion object
    18.       AudioSource.PlayClipAtPoint(explosionSound, transform.position, 1);
    19.       Destroy(gameObject);                        //the radius and finally the explosive lift. Afterwards destroy the game object.
    20.                     }
    21.          }
    22. }
    and the problem is that i want it to apply damage to my enemies but i dont know how to :(
    my enemies tag is 'Enemy' script with health is 'Enemy' and his health variable is called 'Health'

    ive looked everywhere on google and could not find it :(
    if someone could help me i would be very grateful
     
  2. 73cn0109y

    73cn0109y

    Joined:
    Mar 26, 2013
    Posts:
    4
    I may be a little rusty on javascript but have you tried:
    Code (Javascript):
    1. hit.GetComponent(Enemy).health -= power;
     
  3. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    lol i got some cool/weird bug when i add this
    1. hit.GetComponent(Enemy).health -= power;
    2. and no it doesnt apply damage :( only what it does it makes some nice bug the grenade explode and then flies into sky it looks cool but its not what i look for :D
     
  4. 73cn0109y

    73cn0109y

    Joined:
    Mar 26, 2013
    Posts:
    4
    You would change power to how much damage the enemy would take. Could you post you're enemy script, just to rule that out?
     
  5. 73cn0109y

    73cn0109y

    Joined:
    Mar 26, 2013
    Posts:
    4
    Maybe a Collider on the grenade with isTrigger checked. Then a snippet saying something like
    Code (JavaScript):
    1. function OnTriggerEnter(other : Collider){
    2. if(other.Tag == "Enemy"){
    3. other.GetComponent(Enemy).ApplyDamage(*value*);
    4. }
    5. }
    Sorry if I got some parts wrong. Just to give you an idea.

    Edit: The variable "Damage" on your Enemy script may be conflicting with the one in ApplyDamage
     
  6. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    if i set the collider to trigger the grenade is falling under the ground :(
     
  7. 73cn0109y

    73cn0109y

    Joined:
    Mar 26, 2013
    Posts:
    4
    Ok sorry maybe i'm making things worse for your game D: I think im more rusty using unityscript then i thought... Sorry I couldn't help. :(
     
  8. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Any one :( ?
     
  9. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    I got it!!!! i figured out by my self :D as always :(
    i wrote when hit collider.tag == Enemy hit.sendmessage apply damage, damage :D and it worked and that bug was caused because i forgot to destroy the game object :D now it works :D
     
  10. rofikcoga

    rofikcoga

    Joined:
    Apr 8, 2015
    Posts:
    6
    please repost here complete script works
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you really shouldn't call a coroutine from update (at least not without some sort of if control etc.), every frame you are calling a new coroutine so each one will wait however long you've set the yield and then they will all, one after another complete the rest of their code...
     
  12. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Hi, i stopped working on this fps game long time ago and i have no idea where is this folder with this but i will try have a look for you tomorrow for it and i will post it here
     
  13. i3artyy2222

    i3artyy2222

    Joined:
    Aug 18, 2013
    Posts:
    274
    Code (JavaScript):
    1. var radius : float = 3.5;    //provides a radius at which the explosive will effect rigidbodies
    2. var power : float = 10.0;    //provides explosive power
    3. var explosiveLift : float = 1.0; //determines how the explosion reacts. A higher value means rigidbodies will fly upward
    4. var explosiveDelay : float = 5.0; //adds a delay in seconds to our explosive object
    5. var explosionPrefab : Transform;
    6. var explosionSound : AudioClip;
    7. var damage : int = 100;
    8.  
    9. function Start()
    10. {
    11. }
    12.    
    13. function Update () {
    14. Fire();
    15. }
    16. function Fire () {
    17.   yield WaitForSeconds(explosiveDelay);
    18.   Instantiate(explosionPrefab, transform.position, transform.rotation);
    19.   var grenadeOrigin : Vector3 = transform.position;
    20.   var colliders : Collider[] = Physics.OverlapSphere (grenadeOrigin, radius); //this is saying that if any collider within the radius of our object will feel the explosion
    21. for(var hit : Collider in colliders){  //for loop that says if we hit any colliders, then do the following below
    22.    if (hit.GetComponent.<Rigidbody>()){
    23.       hit.GetComponent.<Rigidbody>().AddExplosionForce(power, grenadeOrigin, radius, explosiveLift); //if we hit any rigidbodies then add force based off our power, the position of the explosion object
    24.       //hit.GetComponent(Enemy).Health -= damage;
    25.       AudioSource.PlayClipAtPoint(explosionSound, transform.position, 1);
    26.       Destroy(gameObject);                        //the radius and finally the explosive lift. Afterwards destroy the game object.
    27.          }
    28.       if (hit.GetComponent.<Collider>().tag == "Enemy"){
    29.       hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    30.       AudioSource.PlayClipAtPoint(explosionSound, transform.position, 1);
    31.       Destroy(gameObject);                        //the radius and finally the explosive lift. Afterwards destroy the game object.
    32.          }
    33.       if (hit.GetComponent.<Collider>().tag == "Destructible Things"){
    34.       hit.transform.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    35.       AudioSource.PlayClipAtPoint(explosionSound, transform.position, 1);
    36.       Destroy(gameObject);                        //the radius and finally the explosive lift. Afterwards destroy the game object.
    37.          }
    38.  
    39. }
    40. }