1 decade ago by Joncom
My entity is supposed to move in straight line toward the mouse, but he's not. Here's a working demo to show you what I mean.
Also a screenshot.
EntityPlayer = ig.Entity.extend({
movementspeed: 400,
init: function(x, y, settings) {
this.parent(x, y, settings);
this.addAnim('idle', 1, [0], true);
},
update: function() {
this.parent();
this.move_toward_coord(ig.input.mouse.x, ig.input.mouse.y);
},
move_toward_coord: function(x, y) {
var distance_to_target_x = x - this.pos.x - this.size.x / 2;
var distance_to_target_y = y - this.pos.y - this.size.y / 2;
if(Math.abs(distance_to_target_x) > 1 || Math.abs(distance_to_target_y) > 1) {
this.vel.x = (distance_to_target_x > 1 ? 1 : -1) * this.movementspeed * (Math.abs(distance_to_target_x) / (Math.abs(distance_to_target_x) + Math.abs(distance_to_target_y)));
this.vel.y = (distance_to_target_y > 1 ? 1 : -1) * this.movementspeed * (Math.abs(distance_to_target_y) / (Math.abs(distance_to_target_x) + Math.abs(distance_to_target_y)));
} else {
this.vel.y = 0;
this.vel.x = 0;
}
}
});
Why is my entity not moving in a straight line?
Also a screenshot.

Why is my entity not moving in a straight line?