Log in or register to post comments

Playing animation with unity3d

September 2, 2011 - 12:58pm #1

How can I play animation on an object once a marker is detected?
The object is a child in hierarchy of Image Targets and set to play animation automatically but I can't see the animation but just static rendering of the model.

Playing animation with unity3d

April 13, 2016 - 1:43am #13

I have use this in unity 5.3 (vuforia 5.5)

private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
        Collider[] colliderComponents = GetComponentsInChildren<Collider>();
        Animator[] animatorComponents = GetComponentsInChildren<Animator> (); // I have added this 

	foreach (Animator anim in animatorComponents) {
		anim.Play("Take 001", -1, 0f);
		Debug.Log ("***anim component: " + anim.name);
	}

        // Enable rendering:
	foreach (Renderer component in rendererComponents) {
		if (component.name != "Video" && component.name != "Icon"){
			component.enabled = true;
		}
        }
	.....
    }

 

Re: Playing animation with unity3d

February 25, 2012 - 10:04am #12

Yes it should. Can you post the code that you're using?

Re: Playing animation with unity3d

February 25, 2012 - 9:50am #11

hi guys ive been having the same issues does this code work for multiple items ? or can it work with objects that have the same tag ??

Re: Playing animation with unity3d

February 6, 2012 - 11:01pm #10

I was using the animation field of the TrackableEventHandler script but then I needed to animate 2 objects. That field will only take one object (all animated children will be ignored). Ended up having to also use

GameObject go = GameObject.Find("JEEP");
go.animation.Play("Move");

which worked fine.

Re: Playing animation with unity3d

September 7, 2011 - 10:23am #9

Following code worked for me:

GameObject go = GameObject.Find("JEEP");
		go.animation.Play("Move");

Thanks all for your support.

Re: Playing animation with unity3d

September 7, 2011 - 8:15am #8

Thanks Tyro for your responses.

The animations in my own model didn't show up in the inspector.

To evade the confusion with my own model I tried Spartan King model which is available for free in Unity Asset store. I can see all the animations loaded. But the animation doesn't play when marker is detected.

I have tried this kind of code in TrackableEventHandler without any luck.

private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>();
		Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
		
		
        // Enable rendering:
        foreach (Renderer component in rendererComponents) {
            component.enabled = true;
			Debug.Log("Component name " + component.name );
			if(component.animation)
			{
				component.animation.Play();
				Debug.Log("Animation playing " +  component.animation.isPlaying);
			}
        }

        
    }

Re: Playing animation with unity3d

September 7, 2011 - 7:25am #7
Quote:

What if my animation is baked?

Unity has problems w/ animations that use inverse kinematics (IK). This is why baking to FK ( Forward Kinematics ) is recommended. But you need to inspect the model and remove any IK rigging afterwards. Also it's advised to bake in your native 3D environment (e.g. Maya) and then import. Otherwise baking, in and of itself, isn't a problem.

Have you confirmed that the animation is even importing? Can you see it in the inspector a/o Animation View.

Here's the API for the Animation component.
http://unity3d.com/support/documentation/ScriptReference/Animation.html

Re: Playing animation with unity3d

September 6, 2011 - 8:30pm #6

I'm not sure how baked animations behave differently, you should still be able to loop them.

You can expose script member variables in the Inspector view by making them public. In this case, add this to the top of the TrackableEventHandler script:

public Animation m_animation;

Select your trackable. There should now be a field named "animation" under the attached TrackableEventHandler script. You can drag any game object with an animation component onto this field (e.g. your model).

- Kim

Re: Playing animation with unity3d

September 6, 2011 - 11:43am #5

What if my animation is baked?

How do I pass animation to TrackableEventHandler and where m_animation coming from?

Sorry if asking basic qs as I am noob to unity.

Re: Playing animation with unity3d

September 5, 2011 - 5:46pm #4

Perhaps your animation is only playing once at startup and you're missing it. Trying looping the animation as a test:

http://unity3d.com/support/documentation/ScriptReference/Animation-wrapMode.html

If you want the animation to only play once the marker comes into view you can modify the TrackableEventHandler script provided with the sample applications. Try passing the animation into this script and calling m_animation.Play() in the OnTrackingFound method.

- Kim

Re: Playing animation with unity3d

September 2, 2011 - 5:58pm #3

Have you confirmed that the animation has been imported correctly?

http://unity3d.com/support/documentation/Manual/Character-Animation.html#ImportAnim

Re: Playing animation with unity3d

September 2, 2011 - 1:26pm #2

For more information the animation is baked in the fbx model which I want to play.

Log in or register to post comments