How do I add delay to sprite following mouse cursor?

Get help using Construct 2

Post » Tue Jan 06, 2015 8:45 pm

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?
B
9
S
1
Posts: 12
Reputation: 509

Post » Tue Jan 06, 2015 8:53 pm

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
B
39
S
12
G
11
Posts: 331
Reputation: 7,727

Post » Tue Jan 06, 2015 9:40 pm

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.
B
5
Posts: 12
Reputation: 270

Post » Tue Jan 06, 2015 9:56 pm

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..
B
9
S
1
Posts: 12
Reputation: 509

Post » Wed Jan 07, 2015 4:17 pm

In the end, i just took a nap and relaxed.. Found the solution just after i woke up xD
You do not have the required permissions to view the files attached to this post.
B
9
S
1
Posts: 12
Reputation: 509

Post » Wed Jan 07, 2015 4:26 pm

You should use lerp as @FragFather 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.
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
19
S
5
G
1
Posts: 530
Reputation: 2,727

Post » Wed Jan 07, 2015 4:28 pm

That is really bad solution for several reasons. Do look into lerp (just check the space shooter example project).
B
19
S
6
G
6
Posts: 1,101
Reputation: 5,646

Post » Wed Jan 07, 2015 4:41 pm

He is quite right one must think space shooter is good example
B
252
S
39
G
15
Posts: 1,964
Reputation: 40,753

Post » Wed Jan 07, 2015 6:06 pm

@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.

Capture.PNG


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
You do not have the required permissions to view the files attached to this post.
B
39
S
12
G
11
Posts: 331
Reputation: 7,727

Post » Wed Jan 07, 2015 6:12 pm

brunopalermo wrote:You should use lerp as @FragFather 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.


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

Somebody wrote: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 :)

FragFather wrote:@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.

Capture.PNG


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!
B
9
S
1
Posts: 12
Reputation: 509

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 29 guests