I’ve been searching the web for 4 hours but I still haven’t figured out how to play a single #*!$#-ing animation on my GameObject. Help pls…
using UnityEngine;
using System.Collections;
public class DuelPlayerControllerX : MonoBehaviour
{
/*
public AnimationClip attackTwoAnimation;
public AnimationClip attackThreeAnimation;
public AnimationClip dieOneAnimation;
public AnimationClip dieTwoAnimation;*/
public AnimationClip attackOneAnimation;
void Awake ()
{
}
void Update ()
{
if(Input.GetKeyDown (KeyCode.U))
{
Debug.Log ("U key pushed");
animation.Play("attackOneAnimation");
}
}
}
2 Answers
2
Check that play on awake is not checked for your animations, as you are triggering them via code. Also check that the animations are set to loop (assuming you want to do this) from the import settings.
I dropped your script on a simple door of mine. -Fonted as Code Sample so you can read this easier without me spending 20 minutes editing it, none of this is code ;)-
Here are the settings I have (it plays just fine, as many times as I press ‘U’)
Inspector components
Make sure your script is added to the object with the animations. The animations will be on the parent of the fbx file, so must the script be included there. You should see the animations next to Elements, the name there must match your script.
Play Auto- doesnt matter
Animate physics (mine are off)
culling type - always animate (mine is set to this, you may need it based on renders depending on what you’re doing.)
Import settings:
-RIG-
Animation Type: Legacy
Generation: Stored in Root (deprecated)
-Animations-
Import ani - check
Wrap mode- default
anim. compreeesion - keyframe reduction
errors all set to .5
clips
-----
names with start to end. (door_open) is the name of the animation, so in script
animation.Play("door_open");
Add loop frame - unchecked
wrap mode - default
If that doesn’t work there is an issue with the export. Go back to maya/max/blender and re-export it and make sure the settings there are correct (such as animations being checked and the right amount of keyframes.
Also try adding a different object with a simple animation on it (cube rotating) and try it. Just to make sure it isn’t an issue with your editor.
Good luck!
How old is the model and have you updated to a newer unity since importing it? You may need to check import settings (legacy vs newer methods). You're getting the output "U key pushed" correct?
– HalversonShi HalversonS, Yes, when I push U, the output says it has been pushed. The settings are set to legacy. And yes, I have the Unity updated.
– PaxForceDoes the gameObject play the animation clip if you select it from the animation window within the editor? Also, are you getting any errors or is it just not playing the animation?
– HalversonSYou're code is all fine. Plays animations on my objects (I'm a java guy so I had to check for myself, can't just eye it). It'll have to be either an import setting or something in the editor.
– HalversonS