# Linear movement - Shaking sprite

## Recommended Posts

Hello.

I am porting my WebGL game from Unity3D to Phaser but i have a problem with linear movement of creatures. Sprite is starting to "shake" when moving... I was using 2 diffrent formulas for movement (One is commented in my code bellow) and also trying to round pixels up and down.. (not working).

What can be wrong? I dont want to use physics for movement..

``````function updateCreature(creature, deltaTime) {
var walkTo = creature.walkTo;
if(walkTo == null) {
creature.animations.play('stay');
return;
}

creature.animations.play('run');
var distance = Phaser.Math.distance(walkTo.x, walkTo.y, creature.position.x, creature.position.y);
if (distance < 30 ) {
return;
}

var speed = deltaTime * creature.speed;
var diff = (new Phaser.Point(walkTo.x - creature.position.x, walkTo.y - creature.position.y)).normalize();

creature.position.x += diff.x * speed;
creature.position.y += diff.y * speed;
creature.position.x = Math.round(creature.position.x);
creature.position.y = Math.round(creature.position.y);
// var angle = Math.atan2(walkTo.y - creature.position.y, walkTo.x - creature.position.x);
// creature.position.x += Math.cos(angle) * speed;
// creature.position.y += Math.sin(angle) * speed;

if(creature.scale.x > 0) {
if(creature.position.x < walkTo.x) {
creature.scale.x *= -1;
}
} else if(creature.scale.x < 0) {
if(creature.position.x > walkTo.x) {
creature.scale.x *= -1;
}
}
creature.lastDistance = distance;
}``````