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

0 favourites
From the Asset Store
Players follower. use it for anything which follows the player
  • 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.

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

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

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

    Try adding this:

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

    Try adding this:

    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

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

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

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

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • In fact, just takes a minute. Here is.

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

  • With lerp.

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

    End positions are not 'exact' now, but movement is smooth.

    Speed depends distance.

    Want a constant speed, use Moveto Plugin. I make you an example if needed.

    do you have a capx for rptating the layout using lerp or delta time?

  • I hate it when sprites will not do what I tell them lol!

    OK so I set up a simple test and the reason it overshoots and wobbles is because of the acceleration and deceleration formula 8 directions uses. I played with those numbers and could get it close but not perfect and you want perfect apparently and wobbling is not an option.

    So here is is a simple trick I use all the time using a blank sprite to stop a character sprite exactly where I want them.

    You need a clone of your player sprite with no behaviors and place it where you want your player sprite to stop (in the middle for your game).

    Here are the events:

    I have the player sprite set at 400 speed, 400 acceleration and 0 deceleration. That 0 deceleration keeps the sprite moving back to the center.

    I set the speed to 600 when I release a direction key that way the player goes back faster than it moved away from center and when it overlaps the clone sprite it stops perfectly on center.

    Make the clone invisible and you can use that to have your player return to a specific spot anywhere.

    EDIT: I added in a Start Ignoring Input command to keep your user from clicking keys in rapid fashion that throws off the direction the player is traveling.

    This may be a problem if your player is shooting and you are using arrow keys to shoot. You may need to use a different key for shooting.

  • LaurenceBedford

    You mean something like this ?

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

    Do not ask me to do this with 8Directions. 2Many quirks.

  • Side tip:

    lamar "Every tick" with an "ON..." event has no effect. Drop it and it will work the same way it did with it.

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