- Home /
Some animations aren't playing in Unity 3.4.2
Hello everyone,
I have a player character whose movement works just fine along both the horizontal and vertical axis. Moreover, the character's walk animation works as expected, with all crossfading working as it normally would given that all the model's animation wrap mode is loop. However, the character's punch and kick animations are not playing at all when the relevant buttons are pressed. Here is my PlayerAttack.js script:
var punchSpeed = 1;
var punchHitTime = 0.2;
var punchTime = 0.4;
var punchPosition = new Vector3 (0, 0, 0.8);
var punchRadius = 1.3;
var punchHitPoints = 1;
var kickSpeed = 1;
var kickHitTime = 0.2;
var kickTime = 0.4;
var kickPosition = new Vector3 (0, 0, 0.8);
var kickRadius = 1.3;
var kickHitPoints = 1;
var bashSound : AudioClip;
private var busy = false;
function Start ()
{
animation["punch_r"].speed = punchSpeed;
animation["kick_r"].speed = kickSpeed;
}
function Update ()
{
var controller : ThirdPersonController = GetComponent(ThirdPersonController);
if(!busy && Input.GetButtonDown("Punch"))
{
SendMessage ("DidPunch");
busy = true;
}
if(!busy && Input.GetButtonDown("Kick"))
{
SendMessage ("DidKick");
busy = true;
}
}
function DidPunch ()
{
Debug.Log("Punching...");
animation.CrossFadeQueued("punch_r", 0.1, QueueMode.PlayNow);
yield WaitForSeconds(punchHitTime);
var pos = transform.TransformPoint(punchPosition);
yield WaitForSeconds(punchTime - punchHitTime);
busy = false;
}
function DidKick ()
{
Debug.Log("Kicking...");
animation.CrossFadeQueued("kick_r", 0.1, QueueMode.PlayNow);
yield WaitForSeconds(kickHitTime);
var pos = transform.TransformPoint(kickPosition);
yield WaitForSeconds(kickTime - kickHitTime);
busy = false;
}
The Debug.Log() messages for each attack show up just fine in the console, so I'm 99% certain that it's not a code issue. However, since I checked All Animations in Blender when I exported my .fbx file from Blender 2.5.7 to Unity 3.4.2, I can't figure out how it can be an issue with my Blender file. Any and all help will be appreciated.
MachCUBED
Have you tried setting your characters default animation to punch & kick. This will show you whether the animations are working or not.
I tested the animations by changing the animation name in the following code from my Player$$anonymous$$ovement.js script:
if (Input.GetAxis("Vertical") > 0.1 || Input.GetAxis("Horizontal") > 0.1 || Input.GetAxis("Vertical") < -0.1 || Input.GetAxis("Horizontal") < -0.1)
animation.CrossFade("run");
else
{
animation.CrossFade("idle");
}
}
to play my punch and kick animations during player movement. The animations worked just fine, so it's definitely not an animation problem. Therefore, it must be a code problem.
I assume this script is attached to the object that has the animations?
Yes, both Player$$anonymous$$ovement.js and PlayerAttack.js are attached to the object with the relevant animations.
I assume as you are using Send$$anonymous$$essage("DidPunch"); that you also have a DidPunch function in your Player$$anonymous$$ovement.js script. Do you also have a Debug.Log("Punching...") in that function. If you do, remove it just to eli$$anonymous$$ate the debug log co$$anonymous$$g from there.
Answer by Bunny83 · Dec 11, 2011 at 06:49 PM
I guess your animations work all on the same layer, therefore if you start one of your attack animations it propably gets directly overridden by your movement script. You should set your attack animations to a higher layer so they have a higher priority. If they are finished the movement animation will come back automatically.
Btw. Animation.CrossFadeQueued has some side effects due to duplicating the AnimationState. Only use it if you really need it. CrossFade should be enough in most cases.
Answer by MachCUBED · Dec 11, 2011 at 07:27 PM
Bunny83 and funky llama are right, fixing the Start() function in PlayerAttack.js like so:
function Start () { animation["punch_r"].speed = punchSpeed; animation["kick_r"].speed = kickSpeed;
animation["punch_r"].wrapMode = WrapMode.Once;
animation["kick_r"].wrapMode = WrapMode.Once;
animation["punch_r"].layer = 1;
animation["kick_r"].layer = 1;
}
Fixed everything. Now, the attack animations work as they should. Thank you guys for the help!
Your answer
Follow this Question
Related Questions
Blender import animation to Unity, source take section missing 4 Answers
Blender and unity 1 Answer
Waving flag animation in blender to unity 1 Answer
Problem in Game mode with animation 0 Answers
Blender FBX import problem 0 Answers