I run an event in the camera script and listen to it in the script move the player using this.app. The event conveys the camera Euler angles that are used to set the Euler angles of the player entity. However, I get a delay in rotation the player entity. If I access the script directly using pc.ScriptComponent, then everything works correctly. Can I use listening to an event instead of calling the script directly from the code?
var Mooooooove = pc.createScript('mooooooove');
// initialize code called once per entity
Mooooooove.prototype.initialize = function() {
this.camera = this.app.root.findByName('Camera');
var self = this;
// does not work
this.app.on('gun', function(pitchAngleMax, pitchAngleMin, angle) {
// var targetY = angle.y;
// var targetAngle = new pc.Vec3(0, targetY, 0);
// self.entity.setEulerAngles(targetAngle);
});
};
// works
// Mooooooove.prototype.postUpdate = function(dt) {
// var targetY = this.camera.script.playerCameraMovement.eulers.x;
// var targetAngle = new pc.Vec3(0, targetY, 0);
// this.entity.setEulerAngles(targetAngle);
// };
I’ve ‘fixed’ it but not 100% sure why it fixes the issue. If you fire the event before setting the viewEntity’s rotation in the postUpdate call, it’s fine:
You’ve got a player movement script, a camera movement script, and then the mooooooove script that orients the overall Player entity. All 3 have postUpdate functions that don’t have a defined execution order.
When I run your scene, the mooooooove script postUpdate function is executed before the playerCameraMovement script. The event is essentially being fired when the postUpdate of the playerCameraMovement script has completed (so afterwards).
I think you should simplify how your player movement is handled, to be honest. It’s really hard to see what’s going on.
This is my test project, and maybe I misled a bit.
The playerMovement script is an old script that I want to replace with a script that uses application events. About the order of execution, apparently this is my mistake. Thanks for the help!
This problem arose with the player control script. The weapon control script worked correctly. I changed the code according to your example, and the player control also worked correctly.