[SOLVED] Creating animation component and play it

Hi,

I did the following to create an animation component and then play it programatically. But it’s not playing and also there is no errors in DevTools. What am I doing wrong?

MyScript.prototype.initialize = function () {
var self = this;
this.app.assets.loadFromUrl(’/testAnim.json’, ‘animation’, function (err, asset) {
self.entity.addComponent(‘animation’, {
assets: [asset],
speed: 1
});
console.log(self.entity.animation);
self.entity.animation.play(asset.name);
});
};

I got console.log(self.entity.animation) - ‘model’ and ‘skeleton’ properties are all ‘null’ but I see that ‘assets’ is an array of the correct asset which is ‘testAnim.json’, which I am assuming the animation json is loaded but something wrong either the json file or somewhere. Is this the right way to load an external animation?

** forgot to mention that I used Playcanvas editor to import the FBX w/ animation embedded and downloaded the model json and also animation json manually to use in my code.

Thank you,

Where is testAnim.json file located in relation to the app? (ie what’s your folder structure like?)

My guess is that it can’t find the JSON file to load. On the asset load callback, what is the value of err?

I have a local http server and the /testAnim.json is located in the http root.
Apparently there is no error in the callback (err value is null) because I see self.entity.animation.assets[0] has ‘testAnim.json’ as ‘name’ value. Any ideas? Thank you for looking at this.

Would you mind writing a simple script to use the model JSON and the animation JSON I attached?

Model JSON: https://ufile.io/rez0q
Mapping JSON: https://ufile.io/07u8w
Animation JSON: https://ufile.io/yxfpa

Thank you,

Seems to work fine for me? https://playcanvas.com/editor/scene/578877

Then probably something is wrong in my other code. Thank you so much!

Do you have a model component on the entity?

You are right! I created a parent entity and then attached the script, but made another entity with the model and then attached to the parent. No wonder it doesn’t work! Thank you so much!

1 Like