- Home /
can't play simple animation
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");
}
}
}
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?
hi 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.
Does 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?
You'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.
Answer by BobbyDoogle · Apr 24, 2014 at 06:20 PM
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.
Play automatically won't matter as will if it's set to loop or not.
Answer by HalversonS · Apr 24, 2014 at 06:42 PM
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!
thanks for the advices guys, I'll look at these tomorrow.
HalversonS - thank you for the detailed walkthrough - as a beginner - that was exactly what I needed. Although it works now - I'm bit confused about how Unity works. I HAD TO USE THE NA$$anonymous$$ES OF THE ANI$$anonymous$$ATIONS, ins$$anonymous$$d the names of the variables I assigned the animations to.
The animations are attached to..or assigned to ..(how ever Unity works) to the model of the Warrior game object. Those animations are named eg. AttackOne, AttackTwo, AttackThree. Like this: Element 0 AttackOne Element 1 AttackTwo ...and so on...
I then declared a few variable like this: public AnimationClip attackOneAnimation; public AnimationClip attackTwoAnimation; ...and so on...
In the inspector I then assigned the AttackOne to attackOneAnimation, etc.
my question is: Why can't I work with the names of my variables in my script? Why do I have to use the names of the animations? Weird huh?
Check if your animation is imported as "Legacy". If I try to import as Generic, Humanoid or anytheing else than Legacy, it won't work for me.
PaxForce, animation.play is actually gameObject.animation.play();
So it is searching the gameObject the script is on and looking for an animation, it then looks for and plays any animation on the gameObject that has that name.
If you want to use a variable you have to make sure you reference the gameObject. Having just "gameObject" (which is read in animation.play() -> gameObject.animation.play()) will always look at the gameObject the script is on. Something like this:
var otherGOBJ : GameObject;
function Start(){
otherGOBJ = GameObject.Find("target");
otherGOBJ.animation.Play();
Would be used for playing an animation on an object the script is not on.
Once you understand how objects are referenced and method overloading things click a lot easier. (at least those were my issues starting out)
Hope that helps!
(Remember to mark answers as answered so the Unanswered bin is reduced!)
Also, using a variable as an animation where the variable is a string, you wouldn't use quotations. So it would be just:
animation.Play(attackOneAnimation);
Your answer
Follow this Question
Related Questions
Can the animation editor create local rotational data? 3 Answers
Animation2D help! 1 Answer
Adding animation clips via script 2 Answers
Animator.Play Not Playing Animation - Unity 5 1 Answer
Can't Animate 0 Answers