Can this script move player at straight direction untill they Crash?
var PlayerMovement = pc.createScript('playerMovement');
PlayerMovement.attributes.add('speed', {type: 'number', default: 1});
// initialize code called once per entity
PlayerMovement.prototype.initialize = function() {
// Set initial movement direction
this.direction = new pc.Vec3(0, 0, -1);
};
// update code called every frame
PlayerMovement.prototype.update = function(dt) {
// Check for collision with any objects in front of player
var result = this.entity.raycast(this.entity.getPosition(), this.direction, 1);
if (result) {
// Stop moving forward if collision detected
this.speed = 0;
}
// Move player left or right based on input
if (this.app.keyboard.isPressed(pc.KEY_LEFT)) {
this.entity.translate(-this.speed * dt, 0, 0);
} else if (this.app.keyboard.isPressed(pc.KEY_RIGHT)) {
this.entity.translate(this.speed * dt, 0, 0);
}
// Move player forward
this.entity.translate(0, 0, -this.speed * dt);
};