0 Favourites

How do I make a sprite moving in the mouse direction

  • 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 !

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • 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.
    
    [/code:1gdfjq0f]
    
    and here is the capx [url=https://drive.google.com/open?id=0B0lqVR-ryWBQbXJldGFSLTJYVjg]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
  • 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)