- Home /
Update character's position after animation
Hi there,
I have a character who can perform some basic attacks. If a key is pressed, he'll swipe his sword, if the same key is pressed within a few milliseconds after that first swipe, he'll perform a second swipe. Finally, if the same key is pressed again for a third and final time after that second swipe, he'll perform a spinning strike. So I basically have a combo going on here.
The problem is, after the third swipe, my character moves position, but then reverts back to his old position after the animation. Now I've read that this has to do with the player's mesh moving, but not the gameObject itself. I just can't figure out a solution of how to update his position after the strike. Here's a snippet of my code that focuses on the attacks:
if (Input.GetKeyDown (KeyCode.B) && firstSlash == false && secondSlash == false && SwordCollect.swordObtained == true && blocking == false && attacking==false && HealthController.flinch==false) {
blocking = false;
StartCoroutine (attack ());
} else if (Input.GetKeyDown (KeyCode.B) && firstSlash == true && attacking==false && HealthController.flinch==false) {
blocking = false;
StartCoroutine (attack2 ());
} else if (Input.GetKeyDown (KeyCode.B) && secondSlash == true && attacking==false && HealthController.flinch==false) {
blocking = false;
StartCoroutine (attack3 ());
IEnumerator attack (){
attacking = true;
animation.CrossFade ("slash",0.2f);
audio.PlayOneShot(slash1);
yield return new WaitForSeconds (animation ["slash"].length);
attacking = false;
firstSlash = true;
yield return new WaitForSeconds (0.2f);
firstSlash = false;
}
IEnumerator attack2 (){
attacking = true;
animation.CrossFade ("slash2",0.2f);
audio.PlayOneShot(slash2);
yield return new WaitForSeconds (animation ["slash2"].length);
attacking = false;
secondSlash = true;
yield return new WaitForSeconds (0.2f);
secondSlash = false;
}
IEnumerator attack3 (){
attacking = true;
animation.CrossFade ("slash3",0.2f);
audio.PlayOneShot(slash3);
yield return new WaitForSeconds (animation ["slash3"].length);
attacking = false;
}
Any help or advice would be much appreciated.
Thanks in advance.
Answer by BMayne · Aug 31, 2014 at 04:30 AM
Hey There,
This is a problem with the animation itself. If you moved the game object to correct the position it would just move the animation the same amount. The animation just moves the vertices in the mesh based on a offset from the GameObject.
You can do two things
1) Fix the animation
2) Blend between the end of the third attack and the starting idle animation.
@B$$anonymous$$ayne thanks for your response. How would I go about fixing the animation? What if I want the animation to move my game object forward and not just in one position?