- Home /
Animation error (Last time I'm asking)
if (Input.GetMouseButton (0))
{
anim.play("Swing_Left");
}
if (Input.GetMouseButton (1))
{
anim.play("Swing_Right");
}
Simple enough, right? Well if I press "Swing_Right" in the middle of the animation for "Swing_Left" it cancels out the previous animation. I want one animation to play uninterrupted and reset before another input is activated.
Edit: As it seems no-one on this site seem to know the answer does anyone know where I could possibly get help with this?
Answer by alankemp · Jan 16, 2018 at 10:20 AM
I would use an Animator Controller for this, its a lot easier than doing it in code and a lot more flexible when you want to add more animation states in the future.
The documentation is here: https://docs.unity3d.com/Manual/Animator.html
And there is a video here that explains with an example: https://www.youtube.com/watch?v=JeZkctmoBPw
I am currently using an Animator Controller and as stated it works in every regard except for this one part (that being the fact I don't want animations to be able to be overwritten by another once they start playing).
The question above doesn't look like you're using a controller (since it shows you playing animations, rather than setting flags/triggers). But if you are, then it sounds like you're probably trying to get away with too few states in your controller. If you have an animation that you need to complete, put it in a state that only has one exit transition, that occurs when the animation ends. That exit transition would take you to another state and that's where your controller decides what to do next.
It's true. I am trying to get away with too much with as little scripting as possible and I'm beginning to realize that it isn't going to work no matter how much I want it to. I'm working on a animation controller script right now ins$$anonymous$$d.
Answer by Brachiosaurus · Jan 15, 2018 at 06:00 PM
Try adding an if statement before you tell the animation to play that checks if the previous animation has finished. use this:
if (anim.time == anim.length) {
anim.play("Swing_Left");
}
Can't find a reference for either "time" or "length".
Oh! I'm sorry, I forgot that you need to state the animation as well... maybe try something like this:
if (anim["Swing_Left"].time == anim["Swing_Left"].length) {
anim.play("Swing_Left");
}
Oh, and since you want it to only work at the end of both animations, I would suggest making a string that displays whatever animation is selected/being played at the time. like this:
if (Input.Get$$anonymous$$ouseButton (1))
{
if (anim[Animation].time == anim[Animation].length) {
Animation = "Swing_Right";
anim.play(Animation);
}
}
if (Input.Get$$anonymous$$ouseButton (0))
{
if (anim[Animation].time == anim[Animation].length) {
Animation = "Swing_Left";
anim.play(Animation);
}
}
Your answer
Follow this Question
Related Questions
How can i check if animation has finished playing if the object have no animator attached ? 1 Answer
how to keep track of Lean Tween function? 0 Answers
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
UNITY 2D: How to make a level selection menu (like the video in details)? 1 Answer
Play animation OnTriggerEnter (C#) 1 Answer