Trying to Reset Level using Ghost

I am having a problem with this. I have a ghost and here is my mechanic. My character “Mike” will be transitioning into let’s say failing after colliding the ghost. Is there a code for that?

This is the catch for my game.
I collect coins (at least 5) to transition to a different level
but I don’t know how to make that type of game where you have to restart after colliding with the ghost.
I don’t know any script for that.

What I mean is I am making the interface or different scene that shows “You have to Restart” after colliding with the ghost.

If you want an easy way, you can reload the page.

location.reload();

If you want a nicer way, you can create an UI screen with a replay button. Then you enable this screen when the player fails. On the page below you can find some examples to create a button like that.

https://developer.playcanvas.com/en/user-manual/packs/loading-scenes/

is there a script when I collide with the ghost. It changes you to a different scene.

Yes, the page I shared can help you with that. You can find the scripts in the example projects on the page.

Hi Albertos, I sort off did the transition scene to this
Unfortunately there is a bug when I did it.
Here is the samples.




I need help. and that is the script I used for it.

As the error already shows; this.app.scenesLoadScene() is not a function. Maybe you forget to create that function or you made a typo? I suggest to use the code you can find in the example projects.

I did thanks Albertos. I used a different code
LoadSceneHierarchy

Hello @ETHAN_FOX_CRISTOBAL,

You might want to take a look at this script, which will allow you to load a scene by specifying its name. Create a new script on your project, and paste this code:

/**
 * @name loadScene
 * @function
 * @description Loads a scene's hierarchy and settings.
 * @param {string} sceneName - Name of the scene to load.
 */

function loadScene(sceneName) {
    var app = pc.Application.getApplication();
    var scene = app.scenes.find(sceneName);

    if (!scene) {
        console.log('Scene ' + sceneName + ' not found!');
        return;
    }

    app.scenes.loadSceneData(scene, function (err, scene) {
        if (err) {
            console.log('Error attempting to load Scene ' + sceneName);
            return;
        }

        unloadAllScenes();
        loadSceneContents(scene);
    });
}

/**
 * @name loadSceneContents
 * @function
 * @description Loads a scene's hierarchy and settings.
 * @param {string} scene The scene to load hierarchy and settings from.
 */
function loadSceneContents(scene) {
    loadSceneHierarchy(scene);
    loadSceneSettings(scene);
}

/**
 * @name loadSceneHierarchy
 * @function
 * @description Loads a scene's hierarchy.
 * @param {string} scene The scene to load the hierarchy from.
 */
function loadSceneHierarchy(scene) {
    pc.Application.getApplication().scenes.loadSceneHierarchy(scene, function (err, parent) {
        if (!err) {
            sceneParent = parent;
        } else {
            console.log('Could not load hierarchy!');
        }
    });
}

/**
 * @name loadSceneContents
 * @function
 * @description Loads a scene's settings.
 * @param {string} scene The scene to load settings from.
 */
function loadSceneSettings(scene) {
    pc.Application.getApplication().scenes.loadSceneSettings(scene, function (err) {
        if (err) {
            console.log('Could not load settings!');
        }
    });
}

/**
 * @name unloadAllScenes
 * @function
 * @description Unloads all existing scenes by destroying them in the pc.app.root.
 */
function unloadAllScenes() {
    var app = pc.Application.getApplication();
    while (app.root.children.length > 0) {
        pc.app.root.children[0].destroy();
    }
}

Using this script you could simply add loadScene('menu') to your code, and it would load a scene named menu. It could look like this:

CollisionScript.prototype.onCollisionStart = function(result) {
    if (result.other.name === 'Ghost') {
        loadScene('menu');
    }
};
2 Likes