# [SOL]How do i move an object until reach the mouse position?

Get help using Construct 2

### » Mon Jan 18, 2016 9:53 pm

Hello!
It's my first post here, sorry for anything.

I want to click in some screen location and make the player move until reach that mouse location that i clicked.
How can i do it?

Thanks!
Last edited by Cintra on Tue Jan 19, 2016 7:44 pm, edited 1 time in total.
B
3
Posts: 4
Reputation: 187

### » Mon Jan 18, 2016 11:04 pm

Give the player pathfinding, have an event for when the mouse is clicked to set the pathfinding to that point, then when the path is found make the player walk the path.
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

### » Tue Jan 19, 2016 1:25 am

briggybros wrote:Give the player pathfinding, have an event for when the mouse is clicked to set the pathfinding to that point, then when the path is found make the player walk the path.

But the pathfinding will ignore the wall. I didn't say about these detail, sorry.
The player must receive an impulse and he will only stop if reach the mouse position or some wall.
He need to go in a line.
B
3
Posts: 4
Reputation: 187

### » Tue Jan 19, 2016 1:53 am

This would do it. Note the tab means it's a sub-event.
Code: Select all
`repeat distance(sprite.x,sprite.y,mouse.x,mouse.y) times--- sprite: move 1 pixel at angle(sprite.x,sprite.y,mouse.x,mouse.y) degrees   sprite: overlaps wall   --- stop loop   --- sprite: move -1 pixel at angle(sprite.x,sprite.y,mouse.x,mouse.y) degrees`

Edit:
Opps, that will do it instantly

You can make it move gradually, say 100 pixels per second, with the following:
Code: Select all
`repeat min(distance(sprite.x,sprite.y,mouse.x,mouse.y), 100*dt) times--- sprite: move 1 pixel at angle(sprite.x,sprite.y,mouse.x,mouse.y) degrees   sprite: overlaps wall   --- stop loop   --- sprite: move -1 pixel at angle(sprite.x,sprite.y,mouse.x,mouse.y) degrees`
B
97
S
36
G
131
Posts: 5,513
Reputation: 83,464

### » Tue Jan 19, 2016 7:44 pm

R0J0hound wrote:This would do it. Note the tab means it's a sub-event.
Code: Select all
`repeat distance(sprite.x,sprite.y,mouse.x,mouse.y) times--- sprite: move 1 pixel at angle(sprite.x,sprite.y,mouse.x,mouse.y) degrees   sprite: overlaps wall   --- stop loop   --- sprite: move -1 pixel at angle(sprite.x,sprite.y,mouse.x,mouse.y) degrees`

Edit:
Opps, that will do it instantly

You can make it move gradually, say 100 pixels per second, with the following:
Code: Select all
`repeat min(distance(sprite.x,sprite.y,mouse.x,mouse.y), 100*dt) times--- sprite: move 1 pixel at angle(sprite.x,sprite.y,mouse.x,mouse.y) degrees   sprite: overlaps wall   --- stop loop   --- sprite: move -1 pixel at angle(sprite.x,sprite.y,mouse.x,mouse.y) degrees`

Thanks!
B
3
Posts: 4
Reputation: 187