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