Make a Sprite Move A Fixed Amount Left/Right

Get help using Construct 2

Post » Thu Jun 20, 2013 4:37 pm

Hello!

I am making a small infinite runner game with three 'paths' like Subway Surfers. You will be able to tap left/right to move to the path to either your left or your right. How would I go about making it so that the sprite moves smoothly a fixed amount left or right? (160px, to be precise)

Thank you,

OrangeFlash81
B
8
S
2
G
2
Posts: 49
Reputation: 1,894

Post » Thu Jun 20, 2013 4:41 pm

I assume this is top-down and you're moving along the X axis.

Make a variable "position" and a variable "speed", on start of layout set the "position" variable to the players X position, set the speed to 1 in the beginning. On left or right click, add or subtract 160 to/from the position variable. Then every tick set player position to:

lerp(Self.X, position, dt*speed)

If you need help understanding lerp (linear interpolation) I recommend the links in the FAQ in this subforum.Nimtrix2013-06-20 16:42:51
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Post » Thu Jun 20, 2013 4:48 pm

OK, thanks for the very quick reply! I'll give that a go; I've never tried anything like this before. Can I just as what 'dt' actually does!? I've seen it a lot over the forums...
B
8
S
2
G
2
Posts: 49
Reputation: 1,894

Post » Thu Jun 20, 2013 4:52 pm

Sure thing, it's a bit hard to explain so I'm just gonna redirect you to this handy tutorial.

If you have any problems, feel free to ask and I'll put together an example for you or something.
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Post » Thu Jun 20, 2013 5:00 pm

The script works fantastically! I have another question, and I'll see if you know the answer, @Nimtrix ; How do I make the ScrollTo behaviour focus higher on the object? What I mean is, making my sprite appear at the bottom of the screen rather than bang in the middle. Is this possible?

Thanks?
B
8
S
2
G
2
Posts: 49
Reputation: 1,894

Post » Thu Jun 20, 2013 5:02 pm

You could use the system action

Scroll to X,Y as ;
X = Player.X-(Fixedamount)
Y = Player.Y
B
42
S
17
G
17
Posts: 2,247
Reputation: 17,471

Post » Thu Jun 20, 2013 5:04 pm

Yeah, skip the behaviour and control your camera with events. Use every tick as the condition, then "System -> Scroll To position" actions allows you to set the exact point for the scrolling, and if you use the lerp expression for this as well, you'll get smooth scrolling. (if needed of course)

Then you just set it to scroll to Player.Y - Offsetamount.

Or for smooth:
lerp(ScrollX, Player.X, dt*scrollSpeed)
lerp(ScrollY, Player.Y - offset, dt*scrollSpeed)
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Post » Thu Jun 20, 2013 5:05 pm

Thank you! That method works great, Whiteclaws.
B
8
S
2
G
2
Posts: 49
Reputation: 1,894

Post » Thu Jun 20, 2013 5:07 pm

@Nimtrix , Ninja'd ;)
B
42
S
17
G
17
Posts: 2,247
Reputation: 17,471

Post » Thu Jun 20, 2013 5:10 pm

@Whiteclaws

Indeed, at least I provided some additional info, so not completely useless.
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests