Hi! Im kind of new here because Im using PlayCanvas for school. Its finals season and it was supposed to be due last week. Thankfully it was extended though under sad circumstances.
Anyway, my finals project is a collecting game.
I already have my “player”, “powerup”, and then the 2d screen for the score.
So, what’s expected right is that when the player collides with the powerup, the powerup disappears/or gets destroyed (“entity.destroyed” right) and then there’s an corresponding increment to the score in the 2d screen.
Right? But what’s happening is that- Nothing! Yes I have collision and the rigid body components of both the player and powerup but there’s still nothing happening The player just passes by. (So yes the player can walk)
Here’s the code for player (Note: it has collision and rigid body already):
var CharAnimation = pc.createScript('charAnimation');
// initialize code called once per entity
CharAnimation.prototype.initialize = function() {
//listen for events on the keyboard devicePixelRatio
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown,this);
this.app.keyboard.on(pc.EVENT_KEYUP, this.onKeyUp, this);
};
// update code called every frame
CharAnimation.prototype.update = function(dt) {
var right = this.app.keyboard.isPressed(pc.KEY_RIGHT); //is is ONCE
var forw = this.app.keyboard.wasPressed(pc.KEY_UP);
var right2= this.app.keyboard.wasPressed(pc.KEY_RIGHT); //was IS DURING
var left2= this.app.keyboard.wasPressed(pc.KEY_LEFT);
if(right2) {
this.entity.animation.play('walking');
this.entity.rotateLocal(0,-90,0);
}
if(left2) {
this.entity.animation.play('walking');
this.entity.rotateLocal(0,90,0);
}
//if(right){
// this.entity.animation.play('walking');
//}
if(forw){
this.entity.animation.play('walking');
}
};
// // Event handler function when key is pressed
CharAnimation.prototype.onKeyDown = function (event){
if(event.key === pc.KEY_UP){
this.entity.rigidbody.applyForce(0, 0, 7);
this.entity.translateLocal(0,0,0.20);
}
// if(event.key === pc.KEY_RIGHT){
// this.entity.translateLocal(0,0,0.20);
//}
if(event.key === pc.KEY_LEFT){
this.entity.rigidbody.applyForce(0, 0, -7);
}
if(event.key === pc.KEY_A){
this.entity.rotateLocal(0,-90,0);
}
};
CharAnimation.prototype.onKeyUp = function (event){
if(event.key === pc.KEY_RIGHT){
this.entity.animation.play('idle');
}
if(event.key === pc.KEY_UP){
this.entity.animation.play('idle');
}
};
Now here’s the code for the powerup (Again, it has collision and rigid body):
var Collider = pc.createScript('collider');
var score = 0;
Collider.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
Collider.prototype.onCollisionStart = function(result) {
if(result.other.name == 'player'){
console.log("CONGRATULATIONS!");
score += 1;
var scrDis = this.app.root.findByName('scoreDisplay'); //thats the new name to be short
scrDis.element.text = score.toString(); //number to string ull see type of element sa element ye
this.entity.destroy();
if(score == 5) {
scrDis.element.text = "Pollen Collection Complete!";
}
//Insert Sound code
}
};
Hope this helps anyone understand my problem. I already asked my professor about it and he’s been helping me. I just want to try the forum out for another perspective since he’s very busy which I understand. That’s all thank you! Here’s the link to my project:
https://playcanvas.com/project/785857/overview/finalprjmascarinasodma4