How do I add delay to sprite following mouse cursor?

0 favourites
  • 13 posts
From the Asset Store
Mouse cursor is something that the player controls and sees all the time, that's why it is so important to make it cool!
  • Hi

    Im making a game with an airplane that moves only in Y with mouse. No clicks, drag etc

    I set X where i want and Y to mouse.Y

    The problem is that the plane follows the mouse cursor instantlty. I want to make the plane go slower towards mouse cursor position.

    How can i achive that?

  • Take a look at the LERP function. The tutorial below will give you an idea on how to use it.

    https://www.scirra.com/tutorials/378/cr ... -with-lerp

  • You could keep track of the mouse's current position, OldX and OldY, every half second or so. Then, every time you get new Mouse coordinates, set your Object to old Mouse X and Y. Then, update old Mouse X and Y.

  • I studied and tryied Lerp but ist too troublesome for my current goal. But it is a very good technique, thanks a lot!

    I also tried send it at oldY every 0.5 seconds. The problem is that its not scrolling there, it just appears at the oldY position instantly..

  • In the end, i just took a nap and relaxed.. Found the solution just after i woke up xD

  • You should use lerp as mentioned...

    lerp(currentY, targetY, %distanceToMove)

    For example:

    Every tick >> Set Y to lerp(AirplaneRender.Y, Mouse.Y, 0.1)

    If you want it to move faster, increase the third value, otherwise, decrease it.

  • That is really bad solution for several reasons. Do look into lerp (just check the space shooter example project).

  • He is quite right one must think space shooter is good example

  • nickdtsag

    Glad you found a solution that worked. I can see the benefit of your solution compared to the Lerp function. It appears you want the sprite to move at a constant rate along the Y axis whereas the Lerp function would cause the plane to accelerate more the farther from the Mouse Y it is. Just for fun, I thought I would give you an optimization for your method. You can cut your action count in half (I know, 1 action isn't a big deal but this function can help in a lot of other ways as well) by using the conditional operator. I will post a link to the operators section at the bottom so you can read about what it does.

    [attachment=0:174xc1t2]Capture.PNG[/attachment:174xc1t2]

    This single action will do the same as your 2. Just change one of your conditions to not equal then replace the action to set the AirplaneRender.Y to the following:

    AirplaneRender.Y + (Mouse.Y - AirplaneRender.Y > 0 ? 2 : -2)

    https://www.scirra.com/tutorials/77/nat ... uct-2#h2a0

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You should use lerp as mentioned... <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    lerp(currentY, targetY, %distanceToMove)

    For example:

    Every tick >> Set Y to lerp(AirplaneRender.Y, Mouse.Y, 0.1)

    If you want it to move faster, increase the third value, otherwise, decrease it.

    I tried many combinations and that was the first. My plane moved allong with the mouse sometimes (like before) and sometimes not at all.

    That is really bad solution for several reasons. Do look into lerp (just check the space shooter example project).

    Actually that works perfect for me. I can also adjust the speed by modifying the +2 or -2. And ofc set it to a var if i want and let the player set it.

    Anyway, it works so its fine for me <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

    nickdtsag

    Glad you found a solution that worked. I can see the benefit of your solution compared to the Lerp function. It appears you want the sprite to move at a constant rate along the Y axis whereas the Lerp function would cause the plane to accelerate more the farther from the Mouse Y it is. Just for fun, I thought I would give you an optimization for your method. You can cut your action count in half (I know, 1 action isn't a big deal but this function can help in a lot of other ways as well) by using the conditional operator. I will post a link to the operators section at the bottom so you can read about what it does.

    [attachment=0:ydawftqw]Capture.PNG[/attachment:ydawftqw]

    This single action will do the same as your 2. Just change one of your conditions to not equal then replace the action to set the AirplaneRender.Y to the following:

    AirplaneRender.Y + (Mouse.Y - AirplaneRender.Y > 0 ? 2 : -2)

    https://www.scirra.com/tutorials/77/nat ... uct-2#h2a0

    Exactly!

    And your tip works perfectly as it was before with 2 events! Awesome!

    Thank you all for your time!

  • Actually that works perfect for me. I can also adjust the speed by modifying the +2 or -2. And ofc set it to a var if i want and let the player set it.

    Anyway, it works so its fine for me

    Let me elaborate (now that I'm off-mobile) - the bad thing here is your use of a static "2" number - should your game stutter and a tick is lost the plane will still move just those 2 pixels - whether it's been 1/60th of a second or 1/6th.

    So if you insist on doing it by hand - go ahead, but use dt - delta-time. It is the amount of time passed since last tick, assuming that one tick is 1. So if it was 1/60th of a second the value will be 1/60. Thus, if you wanted to moved 2px 60 times a second it is 120px/per second. So you should change that code to Y =Y + dt*120, for example.

  • nickdtsag Then, do yourself a favor and use your solution together with dt or else it won't be framerate independent...

  • someone thanks for the explanation, it was really helpfull

    i switched to dt

    Thanks brunopalermo

    I did myself the favor :p

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