Help with animation scripting

Hiya,

I’m looking for a bit of help with the connecting an animation script to an fbx file.

My big problem is connecting the animation stored in the file to the script component itself and getting it playing. I believe I understand the logistics of coding it well enough but I haven’t been able to find any good explanations on formatting the fbx on import and connecting it to the script itself. All I’ve had luck with is having the entire animation loop play automatically by checking Play Automatically in the component.

  1. do I declare an Animation at the top and interact with that?

eg. animation.play();

If someone could point me in the right direction or provide a few answers I’d very much appreciate it.

To implement animations on an object, drag the animation clip from your project folder to the object, then apply:

animation.Play("NameOfAnimation");

To the object.

To play the animation at a button press, like say Space, use the following:

function Update()
{
     if(Input.GetKeyDown(KeyCode.Space))
     {
             animation.Play("NameOfAnimation");
     }
}

As always make sure to set “WrapMode” of animation to “Loop” or “Once” depending on desired effect, and make sure you have the checkbox “Animate Automatically” set to your desired preference.

Thanks!

Unfortunately I’m still not getting any interaction with the code…

  1. I have a game object that contains the fbx character
  2. I’ve dragged the take 001 animation onto the object itself
  3. Added a script on the game object shown below
using UnityEngine;
using System.Collections;

public class AnimationScript : MonoBehaviour {
	
	public Animation anim;

	// Use this for initialization
	void Start () {
	
		
		anim.wrapMode = WrapMode.Loop;
		
	}
	
	// Update is called once per frame
	void Update () {
	
		anim.Play("Take 001");
		
	}
}
  1. and dragged the animation into the anim input on the script.

Play Automatically is checked and the animation is Legacy under the rig setting, but upon testing it still only runs once and then stops. I’ve consistently hit this hurdle…

Any insight would be a ton of help!

Much appreciated :slight_smile:

I’m really not that experienced in coding, so I could be completely wrong on this, but yah, I’ll take the chance of looking like an idiot if it chances helping. My guess is perhaps since it’s under “Update”, it might be trying to replay the animation in an extremely fast cycle since it updates every frame and isn’t under an if statement? Perhaps moving the anim.play into the “Start” portion, or changing the Update to FixedUpdate might help. As I said, I’m fairly uneducated with all this, lol, but maybe this can help, I dunno.

Edit
If you still encounter issues after that, the only thing I can ask is if the script is attached to the object, double check in case, and if it is, it might be an issue involving the animation in the fbx itself, because the coding is proper, aside from my cheap little theory above.