Search Unity

How to create Attacking move ?

Discussion in 'Editor & General Support' started by Van32, Aug 31, 2014.

  1. Van32

    Van32

    Joined:
    Jun 23, 2014
    Posts:
    4
    Hey guys, i'm new to unity...
    so i wanna create simple 2D fighting game for training. But i don't know how to stop the animation after i do the attack. The animation just keep repeating, cause i don't know how to change back the value after i hit the button. Can anybody help me?
    and Sorry if my english is bad ....
     
  2. minato20dz

    minato20dz

    Joined:
    Jun 23, 2013
    Posts:
    2
    you can try this
    Animator.SetBool("bool name", Input.GetButtonDown(attack button));
     
  3. Van32

    Van32

    Joined:
    Jun 23, 2014
    Posts:
    4
    thanks for replying

    how to define the "attack button"? i've tried like this
    Animator.SetBool("attack", Input.GetButtonDown(KeyCode.S));
    and get an error said something like can't convert KeyCode to String

    and can u explain how that line works?
     
  4. Chambers

    Chambers

    Joined:
    Apr 4, 2011
    Posts:
    70
    The error you are getting there is because the function GetButtonDown is expecting a string input rather than a KeyCode. If you use the Unity documentation this is explained quite well; http://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html

    I think the issue you are having is with Animation wrap mode which is documented here; http://docs.unity3d.com/ScriptReference/WrapMode.Loop.html if you look at that and try setting the value to false in your code it might fix the problem without you having to alter any input code.
     
  5. Van32

    Van32

    Joined:
    Jun 23, 2014
    Posts:
    4
    I think that not the issue,
    since i just try to do this by watching the unity tutorial (the one that show how to run and jump). i try to do it the same way with the jump. so the code's like this

    if(grounded && Input.GetKeyDown(KeyCode.S))
    {
    anim.SetInteger("attack", 1);
    }

    so when i push S, the animation run, but after that the value still 1, so the animation keep repeating. I think there's a way to change it back to 0 after the animation finish. and I use integer because when i use bool it won't work, don't know why.
    if there's a better way to do that, because somehow i think that's the wrong way to do that, can u give me some link for tutorial or example? or can u explain it to me?
     
  6. Chambers

    Chambers

    Joined:
    Apr 4, 2011
    Posts:
    70
    I've not used that method for playing animations, the way I've done it in the past is like this:

    Code (CSharp):
    1.     void RunAnimation()
    2.     {
    3.         if (playerState == PlayerState.Idle)
    4.         {
    5.             animation.wrapMode = WrapMode.Loop;
    6.             if (playerMode == PlayerMode.OneHand)
    7.             {
    8.                 animation.CrossFade("idle_01");
    9.             }
    10.             if (playerMode == PlayerMode.TwoHand)
    11.             {
    12.                 animation.CrossFade("2h_idle");
    13.             }
    14.         }
    This is the code for idle animations in one of the projects I was prototyping, as you can see I use the animation.CrossFade rather than the function you use. It might be worth trying to use CrossFade, I'm not sure on any tutorials on this subject although I'm sure they probably exist somewhere.

    It might be worth just reading up on the Animation component in the documentation if you can't find a tutorial to help.
     
    Van32 likes this.
  7. Van32

    Van32

    Joined:
    Jun 23, 2014
    Posts:
    4
    Thanks for replying my question and for the advice. I just find the way to solve the case.:D
     
  8. Chambers

    Chambers

    Joined:
    Apr 4, 2011
    Posts:
    70
    No problem! Glad you fixed the issue :)