Hello, and sorry for asking i am a newbie to playcanvas.
I can move the entity “Box2” but when he hit “Box” goes through it, why doesn’t it stop?
https://playcanvas.com/project/937318
“Box” and “Box2” are rigid bodys and collisions.
Thank you for help!
“Box2” → Script
var Movement = pc.createScript('movement');
Movement.attributes.add('speed', { type: 'number', default: 1 });
// initialize code called once per entity
Movement.prototype.initialize = function() {
this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};
// update code called every frame
Movement.prototype.update = function(dt) {
var left = this.app.keyboard.isPressed(pc.KEY_LEFT);
var right = this.app.keyboard.isPressed(pc.KEY_RIGHT);
var up = this.app.keyboard.isPressed(pc.KEY_UP);
var down = this.app.keyboard.isPressed(pc.KEY_DOWN);
if (left) {
this.entity.translate(-this.speed * dt, 0, 0);
}
if (right) {
this.entity.translate(this.speed * dt, 0, 0);
}
if (up) {
this.entity.translate(0, 0, -this.speed * dt);
}
if (down) {
this.entity.translate(0, 0, this.speed * dt);
}
};
// collision logic
Movement.prototype.onCollisionStart = function (result) {
if (result.other.name === 'Box') {
console.log('collision success');
this.entity.destroy();
}
};
// swap method called for script hot-reloading
// inherit your script state here
Movement.prototype.swap = function(old) {
};
// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/