playMaker

Author Topic: Touch and walk  (Read 1860 times)

IIKII

  • Full Member
  • ***
  • Posts: 137
  • Lonely developer
    • Patreon
Touch and walk
« on: October 25, 2019, 01:12:57 AM »
Hi, what are the ways to making a character walk to the touch position on the screen?

Note that it is a 2d side scroller and the character sometimes needs to go up and down slopes with going right/left.

Thinking of using nav agent but isn't it too heavy for mobile..
« Last Edit: October 25, 2019, 03:37:04 AM by iikii »
Support me on Patreon for free adventure games and development insights - https://www.patreon.com/koexstudio

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: Touch and walk
« Reply #1 on: October 25, 2019, 09:07:52 AM »
Well, without plugins, waypoints and anything like that, one solution would be to capture the touch input's coordinates and translate that into scene coordinates (depending on how much of the screen your level takes).
If it's full screen, you wouldn't need to do this transition and could, for example, directly use the coordinates and have the character move on automatic until he reaches them.
Try action Move Towards, tick "ignore vertical" and see what it does.
You also have a vector 2 variant of this action, which might be preferable.
Use a curve action (type curve and see what you get, like curve float which you can synchronize with the beginning of the character's move) to modify the max speed so there's a smooth acceleration. Tweak the curve until it looks neat.

You could even create three states, one of the acceleration at the beginning of the move, one for most of the trip, and one of the deceleration at the end.
The flux would come out of the first state after the character would reach maximum speed (you use a Float Compare to check this FsmFloat max_speed value).
Every time the flux enters a state, use the current post as the "start from" reference. Might require getting the character's current 2D position at the beginning of the state.

in the second state, the middle one, use a finishing distance that makes sense so as to move to the last state which will manage the deceleration.

In the first state (acceleration state), if the character already reaches destination (checked with finishing distance), then go straight to deceleration state (third in the chain) and use the current speed obtained from the first state (that is, the current max_speed value).

Don't insist too much on accel and decel either. The character's displacement has to be smooth but not sluggish either.

IIKII

  • Full Member
  • ***
  • Posts: 137
  • Lonely developer
    • Patreon
Re: Touch and walk
« Reply #2 on: October 25, 2019, 11:38:23 PM »
Well, without plugins, waypoints and anything like that, one solution would be to capture the touch input's coordinates and translate that into scene coordinates (depending on how much of the screen your level takes).
If it's full screen, you wouldn't need to do this transition and could, for example, directly use the coordinates and have the character move on automatic until he reaches them.
Try action Move Towards, tick "ignore vertical" and see what it does.
You also have a vector 2 variant of this action, which might be preferable.
Use a curve action (type curve and see what you get, like curve float which you can synchronize with the beginning of the character's move) to modify the max speed so there's a smooth acceleration. Tweak the curve until it looks neat.

You could even create three states, one of the acceleration at the beginning of the move, one for most of the trip, and one of the deceleration at the end.
The flux would come out of the first state after the character would reach maximum speed (you use a Float Compare to check this FsmFloat max_speed value).
Every time the flux enters a state, use the current post as the "start from" reference. Might require getting the character's current 2D position at the beginning of the state.

in the second state, the middle one, use a finishing distance that makes sense so as to move to the last state which will manage the deceleration.

In the first state (acceleration state), if the character already reaches destination (checked with finishing distance), then go straight to deceleration state (third in the chain) and use the current speed obtained from the first state (that is, the current max_speed value).

Don't insist too much on accel and decel either. The character's displacement has to be smooth but not sluggish either.

Move towards works well for left/right movement but I dont think it will work for up/down stairs kind of gameplay movement. I guess it will be more complicated.
Support me on Patreon for free adventure games and development insights - https://www.patreon.com/koexstudio

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 772
Re: Touch and walk
« Reply #3 on: October 28, 2019, 07:26:30 AM »
Well, try it, and if it fails, maybe use two FSMs to manage the movement, one for X, the other for Y.
In my view there should be no issue with managing the movement of your character in all dimensions with one single action that acts on x, y and z, but who knows?
Surely, if you want your character to climb or jump over fences or holes, you'll have to code that too, likely with volumetric triggers telling your character it's in a specific area of your environment and needs to do something different.

Do you use any waypoint system?