# How do I make the player always stand in the middle?

Get help using Construct 2

### » Sun Feb 12, 2017 6:22 pm

Hello guys! First post here and I know this might be a rather noobish question but I've noticed the community is very nice so I'll shoot.

I'm making a mobile game where the player has to dodge traps, the game will be played vertically with the player going up and traps coming down on him from either side or in the middle. Up until now I've had the player use the 8 Direction behavior restricted to "Left and Right", but now I want him to always be standing in the middle and for controls to make him go either left or right depending on where the player touches the screen and after that go back to the middle.

I would greatly appreciate any input on this. And I do apologize if this has been asked before.
B
4
S
1
Posts: 4
Reputation: 313

### » Sun Feb 12, 2017 10:40 pm

probably 10 different ways to do it

off the top of my head. / if you are using 8 direction then

- (event) if touch is not in touch

- - (sub event) if player .x is less than (left of) middleposition. x
- - (action) then simulate right input ( 8 dir behaviour)

- - (sub event) if player .x is greater than (right of) middleposition. x
- - (action) then simulate left input ( 8 dir behaviour)
NOTE: There is a high a probability that I am wrong.
B
52
S
27
G
7
Posts: 461
Reputation: 8,964

### » Mon Feb 13, 2017 12:21 am

The simple way:

First you need the X coordinate of where you want your player to stand when not being moved. Call it HomeX

On any touch is not in touch ............. Simulate player control pressing right
and Player X < HomeX

On any touch is not in touch ............. Simulate player control pressing left
and Player X > HomeX

That should work if you are just using right and left movement. When you are not touching the screen the player will move back to center.

It works in my head lol!
Banned User
B
29
S
7
G
58
Posts: 1,229
Reputation: 34,910

### » Mon Feb 13, 2017 1:00 am

Thank you to both of you guys! Your input has helped a lot.

However, when the player goes back to the middle he goes beyond the middle and wobbles a bit before centering, as if impulse makes him do so. Is there a way to prevent that? Any kind of Acceleration/Deceleration combination or something?
B
4
S
1
Posts: 4
Reputation: 313

### » Mon Feb 13, 2017 1:33 am

jx5hv4 wrote:Thank you to both of you guys! Your input has helped a lot.

However, when the player goes back to the middle he goes beyond the middle and wobbles a bit before centering, as if impulse makes him do so. Is there a way to prevent that? Any kind of Acceleration/Deceleration combination or something?

Whats wrong with a little wobble? I do it all the time lol!

On any touch is not in touch ............. Simulate player control pressing right
and Player X < HomeX

On any touch is not in touch ............. Simulate player control pressing left
and Player X > HomeX

On any touch not in touch................ Set player 8 Direction controls STOP
and Player X = HomeX

That should stop the player from wobbling.
Banned User
B
29
S
7
G
58
Posts: 1,229
Reputation: 34,910

### » Mon Feb 13, 2017 2:17 am

lamar wrote:
jx5hv4 wrote:Thank you to both of you guys! Your input has helped a lot.

However, when the player goes back to the middle he goes beyond the middle and wobbles a bit before centering, as if impulse makes him do so. Is there a way to prevent that? Any kind of Acceleration/Deceleration combination or something?

Whats wrong with a little wobble? I do it all the time lol!

On any touch is not in touch ............. Simulate player control pressing right
and Player X < HomeX

On any touch is not in touch ............. Simulate player control pressing left
and Player X > HomeX

On any touch not in touch................ Set player 8 Direction controls STOP
and Player X = HomeX

That should stop the player from wobbling.

Thank you, man.

Unfortunately, he still wobbles
B
4
S
1
Posts: 4
Reputation: 313

### » Mon Feb 13, 2017 2:49 am

Hmmmm?

Well try this then:

On any touch is not in touch ............. Simulate player control pressing right
and Player X < HomeX-1

On any touch is not in touch ............. Simulate player control pressing left
and Player X > HomeX+1

The player won't be perfectly centered but that should keep it from overshooting and wobbling.

If that doesn't fix it name your game Wobbler lol!
Banned User
B
29
S
7
G
58
Posts: 1,229
Reputation: 34,910

### » Mon Feb 13, 2017 3:32 am

lamar wrote:Hmmmm?

Well try this then:

On any touch is not in touch ............. Simulate player control pressing right
and Player X < HomeX-1

On any touch is not in touch ............. Simulate player control pressing left
and Player X > HomeX+1

The player won't be perfectly centered but that should keep it from overshooting and wobbling.

If that doesn't fix it name your game Wobbler lol!

Well, I guess my game will be called The Wobbler - TBA.

I'll keep experimenting. Maybe I'm doing something wrong.
B
4
S
1
Posts: 4
Reputation: 313

### » Mon Feb 13, 2017 10:38 am

I thought this might happen. as @lamar suggested making the home a area little bigger should stop it overshooting and correcting but it wont be in the middle. Maybe try making the "deadzone" a bit bigger, put +- 2 or 3 instead of 1

But I reckon if you are serious about doing it like a pro then you will probably want to code it by setting 3 waypoints for the left/middle/right and move to them appropriately on button presses.

there are a number of ways to do it....

I'm thinking you could probably do this very easily and smoothly using a fast lerp

i.e. lerp (currentplayer.x, target waypoint.x, lerpfactor*dt).

Or to use lerp correctly then.... (https://www.scirra.com/blog/ashley/17/u ... delta-time)

otherwise move at a steady pixels per second towards the way point, calculating the distance each time to the way point so you don't overshoot, there should be loads of tutorials on here about moving to waypoints. need to go do some work at work now to make it look like I am doing work but can give some code later tonight...
NOTE: There is a high a probability that I am wrong.
B
52
S
27
G
7
Posts: 461
Reputation: 8,964

### » Mon Feb 13, 2017 11:58 am

With lerp.

https://www.dropbox.com/s/ihs2oe5hv11ap ... .capx?dl=0

End positions are not 'exact' now, but movement is smooth.
Speed depends on distance.

Want a constant speed, use Moveto Plugin. I make you an example if needed.
Last edited by 99Instances2Go on Mon Feb 13, 2017 12:06 pm, edited 1 time in total.
B
33
S
18
G
29
Posts: 2,493
Reputation: 21,450

Next