Search Unity

How to stop enemy getting hurt when weapon just touch him instead of hitting

Discussion in 'Getting Started' started by badassgamer, Jul 19, 2016.

  1. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    The enemy i created getting damaged by the collider even if weapon just touch him and but i want him to recevie damage only when attack animation plays so how to do it?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You need to set some flag that indicates when the attack is happening. You can set this via an animation event, or just do it from whatever script triggers the attack animation. And then, of course, clear this flag after the attack is over.

    Then your damage script just needs to check this "attacking" flag to decide whether to apply damage.
     
    Ryiah, badassgamer and Kiwasi like this.
  3. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    cool so howw to set this flag? need to download from asset store?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, I'm talking about writing a script. You make a script with a "public bool attacking" property, and then set and check this at appropriate times.
     
    Ryiah, badassgamer and Kiwasi like this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You could also disable the collider until the attack animation starts.
     
    Ryiah, badassgamer and JoeStrout like this.
  6. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    Thanks for reply but its confusing as both of you give differnt method dont know which one to try but iam learner when it comes to scripting so i cant do complticated things or program something on my own without help so which one will be suitable for learner in scripting ?
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Use @BoredMormon's suggestion, then — I suspect you can do it with no scripting at all, just enabling/disabling the weapon collider as part of the animation itself.
     
    Ryiah and badassgamer like this.
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you are learning I'd reccomend trying out both methods. Neither of them are difficult. And you will need to learn both eventually.
     
    Ryiah, badassgamer and JoeStrout like this.
  9. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    If you need help with the coding, let us know - I'd be happy to hack some examples together for you. (<- fellow noob since January)
     
    badassgamer likes this.
  10. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    nice and how to disable collider in midddle of game without coding? and tset_tsyung really glad need some examples so thanks all of you
     
  11. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    If you don't want to write code, you need to purchase and learn how to use something like Playmaker that allows you to visually script things. Games run on code, so you have to be able to create it somehow to make one, right?
     
    badassgamer and tedthebug like this.
  12. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    lol but i dint tell i dont want to write code but joe strout says "I suspect you can do it with no scripting at all" so iam asking how its works. and play maker still has coding but in differnt way and both are same
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You might be able to get an animation event to tie in to a unity event which can call SetActive(false) on your collider.

    Maybe.

    If that's possible you could do this with no code.
     
  14. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    @badassgamer Ah yes. I see what I missed now. Sometimes I have a hard time following conversations when the punctuation dies. ;)
     
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Right, using Unity's animation editor, you can change pretty much any property on the object or any child object on any frame of the animation. I believe that includes activating and deactivating things (like the collider, for example).
     
    badassgamer, Ryiah and Kiwasi like this.
  16. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    But my animation is from blender so where should i do it? is there is any tutorial in youtube?
     
  17. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Ah, if you're doing your animations in an external tool and just importing them into Unity, then I don't believe you can just layer on the animation of other properties — you'd have to hook up some animation events or some such. In short, write some code.
     
    badassgamer likes this.
  18. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    ok lets see
    public Collider TriggerCollider;
    Start{
    collider = GetComponent.<SphereCollider>.();
    }
    Update{
    Setanimation(Attack.true)(lol just imagine this is attack animation)
    Triggercollider = Collider.Set.Trigger(true);(so this where we enable collider)
    Setanimation(Attack.false)
    Triggercollider = Collider.SetTrigger(false);
    }
    so this how we should it?
     
  19. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, I was (by this point) suggesting you look into actual Animation Events.
     
    badassgamer likes this.
  20. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    so is there is any videos with this animation event because its not easy to do it myself as your experined programmer you already know but you guys can do it easily without any videos because your good at coding
     
  21. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    i tried the code but it didnt work here is the code
    Code (CSharp):
    1. public Collider Spear;
    2. void Start () {
    3. Spear = GetComponent<Collider> ();
    4. }
    5.  
    6.         if (Input.GetKey (KeyCode.Mouse0)) {
    7.             anim.SetBool ("IsAttacking?", true);
    8.  
    9.             Spear.enabled = true;
     
  22. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Did you really put that if statement outside of any method, as shown? If so, I imagine you get all kinds of compiler errors, right?

    Or have you just extracted those bits from a more complete script? In that case, please show the whole script.
     
    badassgamer and Kiwasi like this.
  23. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    here is whole script and i placed the script in player and also in animation event . or i need to write seperate script for enabling coliider ?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ChracterController : MonoBehaviour {
    5.     static Animator anim;
    6.     public float speed = 10.0f;
    7.     public Collider Spear;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         anim = GetComponent<Animator> ();
    12.         Cursor.lockState = CursorLockMode.Locked;
    13.         Spear = GetComponent<Collider> ();
    14.  
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         float translation = Input.GetAxis("Vertical") * speed;
    20.         float straffe = Input.GetAxis("Horizontal") * speed;
    21.         translation *= Time.deltaTime;
    22.         straffe *= Time.deltaTime;
    23.         transform.Translate (straffe, 0, translation);
    24.  
    25.         if (Input.GetKey (KeyCode.Mouse0)) {
    26.             anim.SetBool ("IsAttacking?", true);
    27.  
    28.             Spear.enabled = true;
    29.        
    30.         }
    31.         else {
    32.             anim.SetBool ("IsAttacking?", false);
    33.         }
    34.         if (translation != 0) {
    35.             anim.SetBool ("IsWalking?", true);
    36.             anim.SetBool ("IsIdle?", false);
    37.         }
    38.  
    39.          else {
    40.             anim.SetBool ("IsWalking?", false);
    41.             anim.SetBool ("IsIdle?", true);
    42.         }
    43.         if (Input.GetKeyDown (KeyCode.Escape)) {
    44.             Cursor.lockState = CursorLockMode.None;
    45.         }
    46.  
    47.     }
    48. }
     
  24. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, good. Now arrange your tabs so you can watch the Animator tab while testing your game. Are the booleans getting properly set? And also watch the Inspector panel for that spear. Is it getting properly enabled and disabled?

    Actually, I see where you enable it; I don't see where you disable it. Perhaps that's the problem: the spear is being enabled but never disabled? We could help you a lot more if you told us what the actual problem is. It's really hard to guess from just "it didnt work".
     
    badassgamer likes this.
  25. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    i turnedoff IsTrigger in spear and then when i play the game and hit the enemy the spear Istrigger is still turned off but the enemy is still recevieng damage
     
  26. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    How? Put a Debug.Log in whatever code applies damage, and then look at the stack trace that appears with it in the Console. This will tell you exactly how that code got invoked.

    (As a side note, if you're using Script Inspector 3, you can even right-click the message in the SI Console, and jump directly to any point in the call stack!)
     
    badassgamer likes this.
  27. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    I did debug.log and it work normally like its says attack on console when i attack enemy.
     
  28. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Great, now look at the stack trace so you can figure out how the code got invoked.
     
    badassgamer likes this.
  29. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    cool and I never used stack trace thing before so its new so how to write it?
     
  30. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You've already written it. Just look carefully at the output in the Console of the debug log. Actually select it, and then don't let your eye skip over anything; study each line that appears around it. The full stack trace is right there.
     
    badassgamer likes this.
  31. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    I found out the problem . i used same attacking script for both player and enemy and when i write not enable it turn off enemy spear which use to as collider to attack player so my enemy cant attack me and thats the problem.
     
  32. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Glad you got it figured out!
     
    badassgamer likes this.
  33. badassgamer

    badassgamer

    Joined:
    Apr 26, 2015
    Posts:
    140
    lol its very hard to do this . i think i need refernce video or do it later
     
  34. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    This is how I'm doing it basically. Only I'm enabling/disabling a bool instead of the collider itself. But in 5.4 events are sometimes skipped due to framerate or animation too fast or whatever so it doesn't always fire them. Its suppose to be fixed in 5.5 though, but haven't tried the beta yet.