How do I make a sprite moving in the mouse direction

0 favourites
  • 3 posts
From the Asset Store
Hand-animated Sprite Base for isometric game/animation development
  • Hey!

    I wanted to know how I can make a sprite ( basically a player here ) moving to the mouse position after clicking, for example like in League Of Legends, where your character will move to the clicked position.

    Thanks !

  • you need 3 localvariables or globalvariables, 3 numbers lets name them targetx and targety for position of mouse, and 1 boolean, that will toggle the movement, lets call it move

    and then we create the bellow pseudo code.

    on left click - set targetx to mouse.x
                  - set targety to mouse.y
                  - set move to 1
    if move =1   player set angle towards position targetx targety
                      - move forward 5 pixels
    /////////////////////////////////        now we add the condition to stop the movement        /////////////////////////////
    compare two variables expression from system and we add the distance formula distance(x1,y1,x2,y2)
    distance(player.x,player.y,targetx,target.y) =< 20  -  set move to 0
    and that is it.
    and here is the capx [url=]move to point[/url]
    you may want to adjust things inside for optimisation like trigger once on distance =< 20 and change the global variables to localvariables on the player so you save events
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Program Files\Construct 2\Examples folder. There's tons of great and ready to use examples, just like "move to mouse.capx".

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)