How to attach a camera to an animation object

Hi!
as an example, I took this solution:

but I don’t understand how it should work:

reparent(parent, [index])

https://developer.playcanvas.com/en/api/pc.Entity.html#reparent)

please give an example code, how should it look right?

Hi @vfxbro,

So you can do it like this:

var bone = this.AnimationBone.findByName("Bone001");
myCamera.reparent(bone);

i have a code:

var CarInit = pc.createScript('carInit');

CarInit.attributes.add('AnimationBone', {
    type:'entity'
});

CarInit.attributes.add('Camera', {
    type:'entity'
});


CarInit.extend({
    
    initialize: function(){
        
        console.log(this.AnimationBone.findByName("Bone001"));
        
        this.AnimationBone.findByName("Bone001").addChild(this.Camera);
    }
    
});

var bone = this.AnimationBone.findByName("Bone001");
myCamera.reparent(bone);

my interface blocks:

what am I doing wrong?

Hi @vfxbro,

Your script structure doesn’t seem correct, study the manual pages on how to write your script code.

Your initialize method should be added to the prototype methods of the script object.

https://developer.playcanvas.com/en/user-manual/scripting/creating-new/

1 Like

Hay @Leonidas! I think @LeXXik uses the same way in his scripts, so I think it’s just possible like this.

1 Like

Yes, I think the extend method will work, but the reparent() call should be placed inside the initialize method, otherwise it will fail.

Yes indeed! Now I see what you mean!

1 Like