Hello! I’m making a cutscene with using the camera manager as used in a tutorial in play Canvas. Its not working and wont switch cameras. Many thank!!
var CameraManager = pc.createScript('cameraManager');
// initialize code called once per entity
CameraManager.prototype.initialize = function() {
this.activeCamera = this.entity.findByName('1');
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
this.on('destroy', function() {
this.app.keyboard.off(pc.EVENT_KEYDOWN, this.onKeyDown, this);
}, this);
};
//prevents default browser actions, such as scrolling when pressing cursor keys
CameraManager.prototype.onKeyDown = function (event) {
event.event.preventDefault();
};
CameraManager.prototype.setCamera = function (cameraName) {
// Disable the currently active camera
this.activeCamera.enabled = false;
// Enable the newly specified camera
this.activeCamera = this.entity.findByName(cameraName);
this.activeCamera.enabled = true;
};
// update code called every frame
CameraManager.prototype.update = function(dt) {
var app = this.app;
};
CameraManager.prototype.update = function(dt) {
setTimeout(function () {
this.setCamera('2');
}.bind(this), 1000);
setTimeout(function () {
this.setCamera('3');
}.bind(this), 1000);
setTimeout(function () {
this.setCamera('1');
}.bind(this), 1000);
};
Hi @SoySauceStudios,
Have you tried to debug your setCamera method?
The code seems correct, given the camera entity names are correct. Check if an entity is indeed found and enabled/disabled.
You can also open the profiler at any point (Alt + T) and check how many active cameras are rendering at a given frame. In your case it should be 1, if everything works as expected.
Hello and thank you for your time! No matter what I try it still seems not to work. Thank you for your efforts to help me and everyone else. Here is my link to my project if you need it.
https://playcanvas.com/editor/scene/1839102
Hi @SoySauceStudios,
I see a couple of issues with the code posted. While the second update function should overwrite the first, your scripts should not contain multiple update functions.
By setting each of your timeouts to 1000ms, they are all being scheduled to change at the same time.
I’m also not sure that you should be using setTimeout inside of an update function. Doing this means that you’re setting a 1 second timer to change to all three of the cameras up to 60 times a seconds meaning the best result you might receive is the cameras rapidly switching between each other in a really uncontrolled way. Consider creating your timeouts in a different function and calling them only when needed.
I hope this is helpful.
1 Like
If you want to switch camera in a specific sequence, you shouldn’t use setTimeout which can’t gurantee the order.
Instead, I strongly recommend to use a custom event queue in which you can make sure one aciton should be completed before the next starts.
Ok thank you! I’m kind of new to play canvas so I was wondering how would you do a custom event?
It’s combination of the observer pattern and queue.