- Home /
Using Animator animation, how to do an animation or start code when an animation finishes?
Hi guys, I know this question has been posted many times but I didn't find the right answer for me. So I have a Monster Gameobject and a character Gameobject and I want that after my character finishes his attack animation my monster start his.
public class BattleScripts : MonoBehaviour
{
public Battle battle;
public CharacterAnimateAction characterAnimate;
public MonsterAnimateAnimation monsterAnimate;
void Start()
{
battle = new Battle();
characterAnimate = GameObject.Find("Character").
GetComponent<CharacterAnimateAction>();
monsterAnimate = GameObject.Find ("Boss").GetComponent<MonsterAnimateAnimation> ();
}
void Update()
{
}
public void PlayerAttack()
{
battle.game.Attack ();
characterAnimate.getCharactersPositions ();
monsterAnimate.DoBossSkill ();
}
in my void player attack the two animations load at the same time and I want to start DoBossSkill when getCharactersPositions() finishes. This is my Character script: public class CharacterAnimateAction : MonoBehaviour {
// Use this for initialization
public Vector3 characterPosition;
public Vector3 monsterPosition;
public MonsterAnimateAnimation monsterAnimate;
Animator anime;
// Use this for initialization
void Start () {
anime = GetComponent<Animator> ();
monsterAnimate = new MonsterAnimateAnimation ();
characterPosition = transform.position;
}
// Update is called once per frame
void Update () {
}
public void getCharactersPositions()
{
GameObject theCharacter = GameObject.Find ("Character");
CharacterAnimateAction characterAnimate = theCharacter.GetComponent<CharacterAnimateAction> ();
characterPosition = characterAnimate.characterPosition;
GameObject theMonster = GameObject.Find ("Boss");
monsterAnimate = theMonster.GetComponent<MonsterAnimateAnimation> ();
monsterPosition = monsterAnimate.monsterPosition;
transform.position = monsterPosition;
AttackAnimation ();
}
If you could help me out I would really appreciate it thank you guys!
Answer by Cherno · Jan 16, 2017 at 07:39 PM
Add an Event to the last frame of the animation clip, or the frame before that, and make it call a function that does whatever you want.
If you just want an animation in the same controller space to play, you can just add a transition to the next animation state and make the animation have an exit time of 1.
I think it is the best solution, the problem was that I had two differents objects with two differents scripts and for each they had their own animation and I couldn't set an event animation from one to another. But I found out a solution which consist to create a $$anonymous$$onster object in my Character script and it worked well!
Answer by mcdahl · Jan 17, 2017 at 03:26 PM
The way I go about this which may not be the prettiest but it works is start a timer when the first animation starts and then when the timer.Elapsed.totalSeconds == the length of the first animation then start the second animation
Thanks for your reply! I did something similar and tried with Coroutine which was really ugly so I decided to go with animator Event and it worked better!
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Cant get rid of animation delay in Mecanim 2 Answers
2D Combo Attack 1 Answer
What's the best art pipeline for 2D animated sprites? 1 Answer
Reusing animation but they sync 1 Answer