https://playcanvas.com/editor/scene/1407296
“Applyforce” didn’t give me exactly what I want.So, i used translate method for moving. Movement is working.But, when player hit the block, it dont stop. I add also collision and rigidbody.
movement.js
var Movement = pc.createScript('movement');
Movement.prototype.initialize = function() {
var isMouseEnter = false;
var lastX;
var isRight;
if (this.app.mouse){
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
}
};
Movement.prototype.onMouseDown = function(event) {
this.isMouseEnter = true;
this.lastX = event.x;
};
Movement.prototype.onMouseMove = function(event, dt) {
if (this.isMouseEnter){
if (event.x > this.lastX){
this.isRight = true;
this.lastX = event.x;
}
if (event.x < this.lastX){
this.isRight = false;
this.lastX = event.x;
}
}
};
Movement.prototype.onMouseUp = function(event) {
this.isMouseEnter = false;
};
Movement.prototype.update = function(dt) {
this.entity.translate(0, 0, dt * -5);
if (this.isMouseEnter){
if (this.isRight === true && this.entity.getPosition().x < 3.5) {
this.entity.translate(15*dt, 0, 0);
}
if (this.isRight === false && this.entity.getPosition().x > -3.5){
this.entity.translate(-15*dt, 0, 0);
}
}
};