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

Problem with Melee System!

Discussion in 'Scripting' started by Mafia2Smoke, Jan 26, 2016.

  1. Mafia2Smoke

    Mafia2Smoke

    Joined:
    Nov 15, 2013
    Posts:
    18
    Hey guys,

    My melee system works in the way that when my melee weapon collides with my enemy it "SendMessages" the damage to the reciever. Now as far as i know there is no "stop" or "dontsendmessage" line of code to tell my enemy to stop "requesting" the damage which in turn makes him "parent/stick" to me.

    Again, any help is appreciated.

    Cheers

    Weapon Script:

    Code (JavaScript):
    1. var dmg:float = 20;
    2. var HitSound : AudioClip;
    3. var damageApplied : boolean;
    4. function OnTriggerEnter(other : Collider){
    5.  
    6.     if(other.gameObject.tag == "Enemy" && !damageApplied)
    7.     {
    8.     other.transform.SendMessage("Damage",dmg);
    9.     damageApplied = true;
    10.     audio.clip = HitSound;
    11.     audio.pitch = Random.Range(0.8, 1.0);
    12.     audio.Play();
    13.     Debug.Log ("Collision!");
    14.     }
    15. }
    16. function OnTriggerExit(other : Collider){
    17.  
    18.     if(other.gameObject.tag == "Enemy")
    19.      {
    20.      damageApplied = false;
    21.      }
    22. }
    Enemy Health:

    Code (JavaScript):
    1. var Damage : AudioClip;
    2. var dead:Transform;
    3. var deadNewPos: Transform;
    4.  
    5. var Health:float = 100;
    6.  
    7. function Damage(dmg:float){
    8.  
    9.     Health -= dmg;
    10.     audio.clip = Damage;
    11.     audio.Play();
    12.     audio.volume = 2.0;
    13.     audio.pitch = Random.Range(0.8, 1.0);
    14.     }
    15.  
    16. function Update(){
    17.  
    18.     if(Health <= 0){
    19.  
    20.     Destroy (gameObject);
    21.     dead = Instantiate(deadNewPos, dead.transform.position, dead.transform.rotation);
    22.     }
    23. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    You're calling the sendmessage in ontriggerenter, it'll only get called once in the first frame the collision happens (for each time it collides).

    You don't have anything in your code here that parents the transforms explicitly, and there is no movement code... so whatever the issue is it's not here.

    What are you seeing happen specifically? how does that differ from what you want?
     
    martinmr likes this.
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    what are that for variables, names are confusing especially with your code when enemy health drops below 0

     
  4. Mafia2Smoke

    Mafia2Smoke

    Joined:
    Nov 15, 2013
    Posts:
    18
    Hey LeftyRighty,

    Whenever i strike my enemy with my weapon which has a collider attached it makes my enemy get stuck to me which is what i want to prevent by telling the enemy to stop requesting the damage from SendMessage. However there IS NO function to get him to stop "requesting" that damage from the function which turn makes him stick to me...
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the code you've posted isn't causing the effect you are seeing. What other code are you using? how have you got your hierarchies setup? etc.
     
  6. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    the enemy does not request anything

    You Hit the enemy with an object(Sword,Fist .. ) when the collider hit, OnTriggerEnter is called ONCE, like it is on ENTER :D
    if the hitted object is tagged as "enemy", Dmg is applied ONCE,

    there is no Parenting of objects.

    like @LeftyRighty is saing, there has to be another Script causing your problem

    have you used transform.parent = transfrom or SetParent() ?
     
  7. Mafia2Smoke

    Mafia2Smoke

    Joined:
    Nov 15, 2013
    Posts:
    18
    Last edited: Feb 19, 2016
    martinmr likes this.
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    :confused: well that answers thread doesn't have the same issue, and just circumvents the sendmessage with a getcomponent (which doesn't answer the original called every frame given the call from a single frame function) but... ok... glad it works I guess