Search Unity

Melee Collision Approach

Discussion in 'Scripting' started by moynzy, Nov 26, 2014.

  1. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    Hey guys, I'm trying to implement some code where when the player attacks and the sword(child of the parent) hits the enemy, the enemy will die.


    Code (CSharp):
    1.     private GameObject enemySword;
    2.  
    3.         enemySword = GameObject.Find("Enemy/Rig/EnemySword");
    4.         if(enemySword == null)
    5.         {
    6.             Debug.Log("could not find");
    7.         }
    This does not work, for some reason.

    I want to reference the sword that is being held.

    What is the best approach for a dirty or clean melee system?

    My approach theoretically, to reference the sword, and see if it's position is the same as the players. if yes thank damaged dealt.

    Opinions would be great.

    Thank you.
     
  2. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    Ack no... you're reinventing the wheel.

    I haven't done this before but my best guess and the way I'd approach this is with colliders or colliders as triggers.

    Put a collider (either a box or a capsule, my first instinct is box.. but thinking it over, a flattened capsule I think is possible and if so, that would make a great sword collider) on each sword. Then make it a trigger by checking the box for it in the inspector.

    Then

    Code (CSharp):
    1. void OnTriggerEnter(Collider c)
    2. {
    3.     if(c.gameObject.tag == "enemySwordTag")
    4.     {
    5.         // we have a hit, do your response
    6.     }
    7. }
    There are at least two beautiful things here.. one: you're using the built in systems.. you always want to use the built in systems.... unless you really really know what you're doing and can like write assembly code in your sleep or something ;)

    Second, you're only checking this stuff when your character's sword actually hits something. That's a nice savings in processing power over some update method or whatever you might use.

    Third (found it :) you have access to the enemy's collider on his sword, through it with c.gameObject, you have access to every part of the enemy. You can call his scripts with c.gameObject.GetComponent<*scriptName*>().CallSomeFunction();

    The most questionable thing about my code/suggestion that you might want to research first is whether to check the collider.gameObject's tag... or find a better method. Tags seem to be for general categories... you might want to check some id that you've defined instead. Depends on how many enemies you're going to have. I haven't had a chance to explore this yet so I don't know the options/benefits/negatives.

    Hope this helped some :)
     
  3. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82
    Hey, thank you for the detailed response with example code :) I will implement this and hopefully have a basic melee combat :)
     
  4. moynzy

    moynzy

    Joined:
    Oct 22, 2014
    Posts:
    82

    Code (CSharp):
    1.     void OnTriggerEnter(Collider other)
    2.     {
    3.         Debug.Log("triggered sword colidr");
    4.         if(other.gameObject.tag=="Player"){
    5.             Debug.LogWarning("hit player");
    6.         }
    7.  
    8.         if(other.CompareTag("Player")){
    9.             Debug.LogWarning("hit player");
    10.         }
    11.     }
    I've attached this to the Enemy Sword. Which is a child.

    I've ticked the trigger.

    When it's hitting me, I can see in the scene view that it's colliding with the "Player".

    Yet, collision does not happen if i stand still. Why is this?

    Do i need to be moving constantly for it to happen?
     
  5. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    If you need to update continually that you are still colliding, then use OnTriggerStay

    Or what you can do is set a boolean flag to true when you get OnTriggerEnter and false when OnTriggerExit happens.

    I'm not sure which is the best method.

    OnTriggerEnter happens when the colliders hit. OnTriggerStay happens almost every frame if the colliders collide. And OnTriggerExit happens when the colliders stop colliding.

    Hope this makes sense, hope it helps.