I want to implement a ball canon to my game. For that, I’ve planned the following features:
- It should have a cooldown feature that prevents me from shooting for like 2 seconds
- The ball should move as if you were to throw it (not straight in a direction, I want to let it bounce around in the levels). It needs to be shot out in the direction I look at.
I have some features implemented already. For those, I want to know if they are implemented correctly:
- Normally, the ball should despawn if it exists longer than 7.5 seconds
- Otherwise, if the ball has detected a collision it should despawn in 3 seconds and play a collision sound.
Here is my code so far:
var Bullet = pc.createScript('Bullet');
// initialize code called once per entity
Bullet.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
this.entity.collision.on('collisionend', this.onCollisionEnd, this);
};
// update code called every frame
Bullet.prototype.update = function(dt) {
if (this.entity.enabled === true) {
this.entity.translateLocal(0,0,-0.4);
setTimeout(function(){
this.entity.destroy();
}.bind(this), 7500);
}
};
Bullet.prototype.onCollisionStart = function (result) {
if (result.other) {
setTimeout(function(){
this.entity.destroy();
}.bind(this), 3000);
}
};
I am grateful in advance for any help I can get.