Load the next scene via trigger box

Since I created my levels in separate scenes, I need to connect them. When the player reaches the end of the level, there should be a trigger box that only detects the collision from the player and switches to the next scene.

Also, can I make a universal script for every trigger box, so I won’t have to manually type in to which level to switch.

Thx

Hi @SayHiToMePls,

Check this tutorial on how to setup and work with triggers:

https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

And here you can find a number of examples on how to change scenes:

https://developer.playcanvas.com/en/tutorials/?tags=scenes

You can try and write a script based on those examples that does what you are looking for.

2 Likes

I’m just wondering:

On the tutorial website, what is the difference between ''changing scenes" and “switching full scene”? As far as I read, both have loaded the new hierarchy and destroyed the other one.

Changing scenes only changes the scene hierarchy. Switching full scene also changes the scene settings such as ambient lighting, the skybox etc

Hello again,

for now, I can switch to the next scene. Now I have the problem that in certain scene, there are entities with scripts that have attributes. If I reach those levels by switching to them, they are not loaded (as far as I guess). If I launch that specific scene, it works normally.

my code so far:

var NextScene = pc.createScript('NextScene');

NextScene.attributes.add("sceneName", {
    type: "string",
    default: "",
    title: "Scene Name to Load"
});

// initialize code called once per entity
NextScene.prototype.initialize = function() {
    //Nächstes Level im Voraus laden, um Ladezeiten möglichst zu vermeiden
    this.scene = this.app.scenes.find(this.sceneName);
    this.app.scenes.loadSceneData(this.scene, function(err, sceneItem) {
        if (err) {
            console.error(err);
        }
    });

    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

NextScene.prototype.onCollisionStart = function(result){
    console.log("Collision detected!");

    //Triggerbox soll erkennen, dass es sich bei der Kollision um den Spieler handelt
    if(result.other.name == "Player"){        
        console.log("Jetzt sollte das nächste Level geladen werden.");
        // Aktuelle Szenenhierarchie vom Speicher löschen
        var oldHierarchy = this.app.root.findByName('Root');
        var self = this;

        // Neue Szene und dessen Einstellungen laden

        // this.app.scenes.loadSceneHierarchy(this.scene, function (err, entity) {
        //     console.log("Szene Geladen!");
        //     this.app.scene.loadSceneSettings(this.scene, function ())
        // });

        self.app.scenes.loadSceneHierarchy(self.scene.url, function (err, scene) {
        if (!err) {
            self.app.scenes.loadSceneSettings(self.scene.url, function (err, scene) {
                if (!err) {
                    oldHierarchy.destroy();
                } else {
                    console.error(err);
                }
            });
        } else {
            console.error(err);
        }
    });
        
    }
};