Point Collection

So I have a script in my game that’s supposed to allow for object collection. However when my player interacts with the collision from the object nothing is happening. I’ve tried to change the tags of which the script uses to identify a “point” but there is still an issue.

Could someone please assist me in this matter, please?

Scoring Script:

var Scoring = pc.createScript('scoring');

Scoring.attributes.add('point', {
    type: 'string',
    default: "point",
    title: 'Tag of Points',
    description: 'What tag is applied to point objects'
});

Scoring.attributes.add('win_number', {
    type: 'number',
    default: 1000,
    title: 'Score to Win',
    description: 'Score to Win'
});

Scoring.attributes.add('collectSound', {
    type: 'string',
    default: "",
    title: 'Collect Sound',
    description: 'Sound to play when collecting'
});

Scoring.attributes.add('score_label', {
    type: 'string',
    default: '',
    title: 'Score Label',
    description: 'Place to display score'
});

Scoring.attributes.add("sceneName", {
    type: "string", 
    default: "", 
    title: "Scene to Load on Win"
});

var score = 0;
// initialize code called once per entity
Scoring.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};


Scoring.prototype.onCollisionStart = function(result){
    if(result.other.tags.has(this.point)){
        score = score + 1;
        result.other.destroy();
        
        if (this.collectSound !== "CoinGet") {
            this.entity.sound.play(this.collectSound);            
        }

        
        if (this.score_label !== "") {
            var scoreDisplay = this.app.root.findByName(this.score_label);
            scoreDisplay.element.text = score.toString();
        }

    }
};

// update code called every frame
Scoring.prototype.update = function(dt) {
    if (score >= this.win_number) {
            var self = this;
        
            //this.entity.script.playerMovement.enabled = false;
            //var anim = this.entity.findByName('Model');
            //anim.script.playerAnimationHandler.enabled = false;
            this.app.keyboard.off(pc.EVENT_KEYDOWN, this.keyChange, this);
            this.app.keyboard.off(pc.EVENT_KEYUP, this.keyChange, this);
            
            self.loadScene(self.sceneName);
    }
};


Scoring.prototype.loadScene = function (sceneName) {
    // Get a reference to the scene's root object
    var oldHierarchy = this.app.root.findByName ('Root');
    
    // Get the path to the scene
    var scene = this.app.scenes.find(sceneName);
    oldHierarchy.destroy();
    // Load the scenes entity hierarchy
    this.app.scenes.loadSceneHierarchy(scene.url, function (err, parent) {
        if (!err) {
            //oldHierarchy.destroy();
        } else {
            console.error(err);
        }
    });
};

// swap method called for script hot-reloading
// inherit your script state here
// Scoring.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Project Link: PlayCanvas 3D HTML5 Game Engine

Your coin entity doesn’t have a rigidbody component. Try to add this component and check the result. If you don’t want to use a rigidbody then you have to use the trigger event instead of the collision event.

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

1 Like

Alright it’s working in terms of collection, however, it’s not playing the sound that was set for it.

Could you check in the console of your browser if the value of this.collectSound is CoinGet or not?

console.log(this.collectSound);

@Overbaked, in addition to @Albertos’s answer, have you set up the audio for the scene correctly? You can check this tutorial to clarify. Additionally, I’m assuming this.collectSound is an attribute? Have you parsed the script in the editor and chosen the correct sound? If it’s not an attribute, where are you setting the value of the collectSound object?