Attaching camera to fbx conatining animaton

hey everyone
I want to attach a camera to an fbx file containing animation.
however it seems that the camera is just stuck on the origin of the scene ,what am I doing wrong?

heres my code:

var CamerTest = pc.createScript('camerTest');

// initialize code called once per entity
CamerTest.prototype.initialize = function() {
    
};

// update code called every frame
CamerTest.prototype.update = function(dt) {
    
     var camTarget1 = this.app.root.findByName('camTest');
    
     var maxObject = camTarget1.model.model.graph.findByName('Camera');
     
    var camPosition = maxObject.getPosition();
    
     var camRotation = maxObject.getEulerAngles();
    
    console.log("postion: " + camPosition);
    console.log("rotation: " + camRotation);
    
    this.entity.setPosition(camPosition);
    this.entity.setEulerAngles(camRotation);
};

hope someone can help
best

Do you get any errors in the console?

yes on line 15 I get:
: Cannot read property ‘getPosition’ of null

Im not sure what that means?

That probably means it can’t find the object referenced

 var maxObject = camTarget1.model.model.graph.findByName('Camera');

i posted a photo
ignore the other cameras and paths theyre disabled.

Ive attahced the above script to “Camera” - the child of “camTest”. shouldnt it work? playcanvas

@linelineline you just want to find the camera under the camTest entity right? In that case, there is no need to do model.model.graph.findByName.

A simple call of camTarget1.findByName('Camera') should suffice.

@DevilZ

If I attach my script to camTest and perform your suggestion, my camera just attach itself to the pivot point of the animated object and rotates like crazy. its not following along the actual object.

I was using the example of this guy https://playcanvas.com/editor/scene/628464

thats why I attached the script to the child and not the parent but its not working

I’m on my phone now, so will check it out later today.

thanks @DevilZ

I am just looking for a script that will attach the camera to the moving object (and not the pivot point of the fbx which is center of the scene)

Can you share the project for us to take a look at?

It would be great if you do that @linelineline. In fact, now that I understand your intent, if you add me to the project, I can try to help you fix it. Username - tester29.

thanks a lot DevilZ! will do that

also is it possible to animate the rotation of the camera? I dont wish for it to be controlled by input but rather just rotating alongside a pretermined set of coordinates

Could you explain this a bit further?

hey @DevilZ i wrote you a pm

1 Like

https://playcanvas.com/editor/scene/932401

Here’s the fixed version: https://playcanvas.com/editor/scene/933245

The issue was that the script wasn’t using the animated graphNode in the camTest model and also the position/rotation of the animated node wasn’t being given to the camera.

Script:

var CamerTest = pc.createScript('camerTest');
CamerTest.attributes.add('followObject', {type: 'entity'});

// initialize code called once per entity
CamerTest.prototype.initialize = function() {
    this.camTarget1 = this.entity.findByName('Sphere');
};

// update code called every frame
CamerTest.prototype.update = function(dt) {
    var camPosition = this.camTarget1.getPosition();
    var camRotation = this.camTarget1.getEulerAngles();

    this.followObject.setPosition(camPosition);
    this.followObject.setEulerAngles(camRotation);
};