Im trying to make an easily expandable script to change scenes & I asked chatgpt for help but event hat script still isn’t working. This is what I have so far
var LevelSwitcher = pc.createScript('levelSwitcher');
// Initialize the script
LevelSwitcher.prototype.initialize = function () {
this.entity.collision.on('collisionstart', this.readyToSwitch, this);
};
// Switch level to a new scene by name
LevelSwitcher.prototype.switchLevel = function (sceneName) {
var app = this.app;
// Find the scene by name
var scene = app.scenes.find(sceneName);
if (scene) {
app.scenes.loadSceneHierarchy(scene.url, function (err, loadedScene) {
if (err) {
console.error("Failed to load scene:", err);
} else {
console.log(`Scene switched to: ${sceneName}`);
}
});
} else {
console.error(`Scene not found: ${sceneName}`);
}
};
// Collision callback to handle level switching
LevelSwitcher.prototype.readyToSwitch = function (otherEntity) {
if (otherEntity.name === "Player") {
console.log("Collision detected with Player");
// Check the current scene name
var currentSceneName = this.app.root.name;
console.log(`Current Scene Name: ${currentSceneName}`);
// Determine the next scene to load
if (currentSceneName === "lvl 1") {
this.switchLevel("lvl 2");
} else {
console.error("Scene transition logic not defined for this scene.");
}
} else {
console.log(`Collision detected with: ${otherEntity.name}`);
}
};