Alright I've got a combat system I want to use for my game that utilizes button presses at specific times. I am asking for help but also just using this post to get the ideas out of my head and onto paper. I have done research but no one seems to have quite what I am looking for so I'm trying to see if anyone here can help. *added note* I can not use the mechanim system to achieve what I am looking for. Later on in the process I will be switching out animations which the mechanim system does not allow for. So the basics of what I am trying to develop. I am starting with 3 animations to keep things easy, the animations are intended to play in sequential order. The tricky part is I want to the player to not simple button mash to get a combo off, but actually have to click at the proper moment to trigger the next attack. So if the player starts in idle and I click, I start the first animation, if I click again while the animation is playing it will move to animation 2 when animation 1 is done. If the player clicks two or more times while animation 1 is playing, then only animation 2 will still play but not animation 3. The player still has to click during animation 2 to get to animation 3. How I am planning on doing this is setting up a simple switch that is true or false between animations. So in idle the click bool is set to false. Basically something like this: CLICK set bool to TRUE if bool is TRUE play animation1 This part I know how to do, no issues there, its getting the clicks to coincide with the frames of the animation that I need to figure out. Right now each animation is set to 50 frames. I think it will work best if at the start of each new animation the CLICK bool is set to false, and only if the CLICK bool is set to true will the next animation in the sequence play. It seems like the easiest and most logical way to me. CLICK set bool to TRUE if bool is TRUE play animation1 when animation1 starts set CLICK bool to false if CLICK bool is false at the end of animation1 return to idle animation if CLICK bool is true at the end of animation1 play animation2 so on and so forth. It seems like a simple way to do it, but my problem is I do not know how to code this. I know how to play animations and get input from buttons and keys, but I don't know how to show the length of each animation in code or how to check if an animation i still playing or not. Any help at all from anyone would be great, thanks!
If youre using mecanim, you can trigger animations just by changing a simple bool, int or float in the Animator. Play around with that
True, sorry I should have added that I don't want to use the mechanim system. Mechanim does not allow animations to be swapped out, which is what I eventually will be doing with this system. So unfortunately I have to code things the hard way.
Code (csharp): public int animationCounter = 1; void Update() { if (Input.GetButtonDown("Fire1")) { if (animationCounter == 1) { GetComponent<Animation>().Play("SwingDown"); animationCounter += 1; } else if (animationCounter == 2) { GetComponent<Animation>().Play("SwingSide"); animationCounter += 1; } else if (animationCounter == 3) { GetComponent<Animation>().Play("SwingStab"); } Removed the clickAction. Isn't needed at all really, at least not right now. I may have to put it back in later. So now things play correctly, but if I click quickly I will cycle to the proper animation, stop the current animation, and play the selected animation. I need to figure out to stop the animations from overlapping. Any ideas?
Code (csharp): public int animationCounter = 1; public bool clickAction = false; void Update() { if (Input.GetButtonDown("Fire1")) { clickAction = true; } if (clickAction == true) { if (animationCounter == 1) { GetComponent<Animation>().Play("SwingDown"); animationCounter += 1; clickAction = false; } else if (animationCounter == 2) { GetComponent<Animation>().Play("SwingSide"); animationCounter += 1; clickAction = false; } else if (animationCounter == 3) { GetComponent<Animation>().Play("SwingStab"); clickAction = false; } Alright so I do have to add the clickAction back in. I know later it will be important because the player has to click while the animation is playing to get to the next animation, rather than the number just cycling up the animation number. Still trying to solve the problem of the animations, but I'll get on that.
Changing the Play to CrossFade sort of made it work, but not quite what I want. The animations play correctly when I click, but it plays an animation for each time I have clicked, regardless of what is happening with the current animation. So that won't work.
Drop an Animator in that and try the New mecanim animation system. Its great! you can do like Animator.setFloat("test",1); Or Animator.getFloat("test") and in the unity editor you can add logic to automagically switch Animation according to float value. So you can get the float and use it to read/write the current animation state.
Can't use mechanim because what I want to do later with this system mechanim can't do. wow I have no idea what any of that is haha. so the Animator.setFloat, will that get the length of the animation to use later? What will the Animationsfilm do? Not sure I follow, sorry.
Corrected my autocorrect typo, sorry Animators are great because they can automatically switch animations according to different given parameters. But if you cant use mecanim i think you cannot use the Animator. But lookup, probably i'm wrong. http://docs.unity3d.com/Manual/Animator.html Maybe you should try counting mouse clicks while the animation is playing. Different scripting improvement idea: public int animationCounter = 1; public bool clicked = false; public bool clickAction = false; void Update() { if (Input.GetButtonDown("Fire1")) { clickAction = true; } if (clickAction == true) { if (animationCounter == 1) { GetComponent<Animation>().Play("SwingDown"); if (clicked == false) { animationCounter += 1; } else { //user clicked second/multiple time, do something fancy } clickAction = false; clicked = true; } }
Haha its alright. I'll look up how to use the animator better though. I have one attached but I only attached it to list the animations. Yeah its annoying that mechanim won't work. Later down the road animations will be switched out, and mechanim won't allow you to switch out animations in a sequence. Counting clicks might work, but if someone is button mashing then they can get around it, which is what I don't want. Makes the system more difficult to create for sure, but in the end requires the played to not just button mash to play.
Hm tried it that way but it didn't seem to do much. the system I have now is perfect except that the animations overlap, if I can solve that I should be able to get the whole thing solved.