- Home /
Animation blending/crossfading sword help
#pragma strict
var swordSpeed = 0.5;
var slash: AudioClip[];
private var t:float;
var head:CharacterController;
function Start(){SwordBobble();}
function SwordBobble(){
var sword = gameObject.Find("blade");
var ani = sword.animation;
while(true){
if(head.isGrounded && head.velocity.magnitude >= 4){ //&& !animation.animations.swordslash.isPlaying){
ani["SwordBobble"].speed = 1;
ani.Play("SwordBobble");
//ani.CrossFade("SwordBobble");
yield WaitForSeconds(0.001);
}
else{
ani["SwordBobble"].speed = 0;
//animation.Stop();
yield;
}
}
}
function Update(){
if(GameObject.Find("Gameover") == null){Disable();}
t += Time.deltaTime;
if(t >= 0.5 && GameObject.Find("Enabled") == null && GameObject.Find("blade") != null && GameObject.Find("Gameover") != null && GameObject.Find("PauseNo")!= null && GameObject.Find("GUI_black").guiTexture.color.a <= 0){
if(Input.GetButtonDown("Fire1")){
var sword = gameObject.Find("blade");
if(sword != null && GameObject.Find("SlashFalse") != null){
//var ani = sword.animation;
var anistate = sword.animation["swordslash"];
sword.animation.Play("swordslash");
audio.pitch = Random.Range(1.0 - 0.05, 1.0 + 0.05);
var ran = slash[Random.Range(0,slash.Length)];
audio.clip = ran;
audio.Play();
t = 0;
}
}
}
}
I've been having a bit of trouble lately. As you can see, I have two main functions of this script: one triggers a SwordBobble, like where the sword bounces around while walking, and the second function is to trigger a sword slashing animation, which plays when triggered by Fire1.
My big problem is that while I am walking, the SwordBobble animation is playing, so when I try to play a sword slash animation, it's being overlapped by SwordBobble.
I don't know the difference between crossfade and blending exactly - is blending to literally mix in two or more animations and crossfade just "fades" out an animation? What would that look like? I'm confused on what they do.
So, my goal here is to make it so SwordSlash will, I guess "blend" in with SwordBobble, and then play the swordslash animation, and then once the swordslash animation is finished, it will automatically go back/blend back to SwordBobble mode.
Swordslash only plays when I am not moving, and I need it so it can play while moving as SwordBobble is playing also.
I don't know how I can set up these blending and crossfading functions, so may someone help me on how I can accomplish this? Please do not speak with a lot of elitism, as I usually can't follow what you're saying. Thank you very much for taking the time to read my frustration.
Answer by MibZ · Dec 07, 2012 at 10:01 PM
I haven't done that much work with animation blending but I have read a lot about it in anticipation of a future project. The layer value of an animation determines how important it is, if you set the layer value of your idle animations to -1, any animation with a layer higher than it should be given a higher priority and fade over the idle.
I'm not 100% sure as I haven't done it before, but I distinctly remember reading that and it is a very quick thing to try, hate to see somebody ask a question and get no responses at all, so I figure this is more valuable than nothing.
Oh. Even though you haven't done much with animation, can you explain to me what animation crossfade and blending does? I'm trying to get them to work but it seems that I just don't know how to use them because I don't exactly know what they do.
From my understanding blending is used to move towards a specific part of the animation over time without effecting other animations and crossfading is used to make a smooth transition from one animation to another (effecting both animations) ins$$anonymous$$d of immediately stopping animation A and starting animation B.
Okay, so I probably need to use CrossFade in order to transition from the swordbobbling to the swordslashing, and vice versa.
I can't seem to get this working.
Never$$anonymous$$d, I finally got the whole thing working. I unfortunately found out I had another script enabled that screwed around with my script.
Answer by Dyon09 · Dec 09, 2012 at 12:58 AM
I tried using a variation of this script for a project that I have, but Unity classifies 'Disable' as an "Unknown Identifier"
Yeah sorry, I removed my function Disable(){ thing because it was entirely unrelated to the help I needed.
Your answer
Follow this Question
Related Questions
what is difference of animation blend and animation crossfade? 0 Answers
Animation.Play blends previous animation 1 Answer
How to play multiple animations on the same game object. 2 Answers
How can I smoothly transition my player camera from manual control into an animation? 1 Answer
Animation Stop and Stay 1 Answer