- Home /
Animation blending. (two animations, same mesh, same time)
Hey, this one has really got me going! I've been on here for a little over a month now and you guys have taught me so much but I'm having a little trouble blending my animations together. Here's what I want to accomplish:
When the D key is pressed, my character walks with the "walk" animation. Everything is fine here. My character has a sword, the sword animation is named "swordswing". Both animations play fine, I have double checked this. What I want is when the character is walking, if the key "P" is pressed, the animation "swordswing" plays over the top of the walking animation (but I want the legs to still move). At the moment the animation plays seperatley (stops the walking) and initiates the swordswing.
At the moment, the animation cancels the other one out, the swordswing animation has no keyframe movement on the legs so shouldn't the animation carry on? Have I done this correctly?
I want my character to swing the sword but his legs continue to move as he does so. I know this is done by blending but I can't see where I have gone wrong, any help will be hugely appreciated!
Sorry for the messy code! I have not formatted it yet.
var acceleration : float = 100;
var move : float;
var doubleJump : float = 0;
var z : float;
var left : Transform;
var right : Transform;
var health : int = 100;
var jumpDelay : boolean = false;
function Start () {
jumpDelay = false;
animation.wrapMode = WrapMode.Loop;
animation["jump"].wrapMode = WrapMode.Once;
animation["swordswing"].wrapMode = WrapMode.Once;
animation["jump"].layer = 1;
animation["swordswing"].layer = 1;
animation["swordswing"].weight=0.4f;
animation.Stop();
}
function Update () {
//Movement without guns goes here
Physics.gravity = Vector3( 0, -200, 0);
animation.CrossFadeQueued("random");
if( Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D))
{
acceleration = 100;
animation.Play("walk");
}
else if( Input.GetKeyDown(KeyCode.P))
{
animation.Blend("swordswing");
}
else if( Input.GetKeyDown(KeyCode.Space) && jumpDelay == false )
{
Jump();
animation.CrossFade("jump");
animation.CrossFadeQueued("idle");
jumpTimer();
}
else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.A))
{
animation.CrossFade("idle");
}
//Stop the moonwalking bug
if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A))
{
acceleration = 0;
animation.CrossFade("idle");
}
else if (Input.GetKey(KeyCode.D))
{
acceleration = 100;
animation.CrossFade("walk");
}
if (health == 0){
animation.Play("death");
animation.wrapMode = WrapMode.Once;
health = 100;
}
move = Input.GetAxis("Horizontal") * acceleration * Time.deltaTime;
transform.Translate (0, 0, move);
}
function Jump(){
doubleJump +=1;
if( doubleJump == 1 )
{
rigidbody.velocity.y = 175;
}
}
function OnCollisionStay (collision : Collision){
doubleJump = 0;
}
function OnCollisionExit (collision : Collision){
doubleJump = 1;
}
function jumpTimer(){
if (animation.IsPlaying("jump"))
jumpDelay = true;
yield WaitForSeconds(3);
jumpDelay = false;
}
I will be here for a while so feel free to ask for any additional information.
Summary: I want the sword animation to play at the same time as the walking animation, without stopping my characters legs from moving. I really need this fixing and any guidance will be greatly appreciated. Thank you.
Additional information:
Sword animation : swordswing Walking animation: walk
Please note: I have read a lot of documentation on this but unfortunately, I cannot take anything from it for my script :/
Answer by Mrobertson · Oct 16, 2012 at 10:38 PM
It sounds like you will have to use AnimationState.AddMixingTransform to have your sword swing animation only affect the upper body joints
From the Documentation
/// Adds a mixing transform
var shoulder : Transform;
animation["wave_hand"].AddMixingTransform(shoulder);
Thanks buddy, would the wave hand be the swordwing animation and what would replace "shoulder"?
Shoulder here would be the transform of the highest bone in the hierarchy that you want the animation to affect - most likely whatever bone you have as your character's shoulder.
wave_hand would be your swordswing animation, yeah.
Alright, we have progress! It's now working for my A key (when I walk the other way ^^ ) I will hammer it a bit more try and iron out the bug, either way, I'll be back to rep you both :)
Brilliant got it working! Thank you very much! I'm unsure if I can mark 2 answers as correct on Unity but if I can, post yours as an answer as well. Both played a part in fixing this. Seriously I cannot tell you how grateful I am :D I thought this would be the downfall of me lol :)
Your answer
Follow this Question
Related Questions
Playing 2 animations at the same time 2 Answers
How do I play muiltiple animations? 1 Answer
How can I read time from clip that is PlayQueued? 1 Answer
two projects in the same time 4 Answers
Animation.Time help 1 Answer