# How do I Smoothly Move an Entity to the Position of Mouse?

Get help using Construct 2

### » Thu Jun 05, 2014 4:31 am

Hello everyone
How do I implement this code shown in this example into Construct 2 ?
I tried to use event "every tick move sprite at angle (sprite.x,sprite.y,mouse.x,mouse.y) a distance of 300*dt" and when the sprite reaches the mouse position it vibrates, i need to create the same smooth movement with easing reach as shown in the example.
Thanks.

function gameLoop(){
var xDistance = mouseX - ship.x;
var yDistance = mouseY - ship.y;
var distance = Math.sqrt(xDistance * xDistance + yDistance * yDistance);
if (distance > 1) {
ship.x += xDistance * easingAmount;
ship.y += yDistance * easingAmount;
}
}
Last edited by Egyptoon on Thu Jun 05, 2014 4:34 am, edited 1 time in total.
B
15
S
5
Posts: 192
Reputation: 1,514

### » Thu Jun 05, 2014 4:33 am

URLS removed, any way here is the code

function gameLoop(){
var xDistance = mouseX - ship.x;
var yDistance = mouseY - ship.y;
var distance = Math.sqrt(xDistance * xDistance + yDistance * yDistance);
if (distance > 1) {
ship.x += xDistance * easingAmount;
ship.y += yDistance * easingAmount;
}
}
B
15
S
5
Posts: 192
Reputation: 1,514

### » Fri Jun 06, 2014 12:18 am

Have you considered using lerp or easetween
lerp(a, b, x) Linear interpolation of a to b by x. Calculates a + x * (b - a).

http://www.scirra.com/forum/behavior-ea ... 53288.html
What I know would fill a Book , what I don't know would fill a Library
B
37
S
12
G
29
Posts: 331
Reputation: 16,499

### » Fri Jun 06, 2014 1:29 am

PERFECT! "lerp" is amazing,
thanks alot
B
15
S
5
Posts: 192
Reputation: 1,514