Moving Character Left/Right Across A Path

1 favourites
  • 12 posts
From the Asset Store
A complete set of graphical user interface (GUI) to build 2D video games. With wood, nature, & jungle theme
  • I'm looking to create a game where the player moves the character along a restricted path on the game screen. The player can either SWIPE for quickest movement or hold Left/Right on the screen to move that direction, going in either direction to dodge and attack.

    The paths I'm thinking of are: short wide 'U' shape, short wide 'V' shape, a horizontal line and an upside down short wide 'T' shape (which would be one long path that goes from Left to Up to Down to Right. I can see creating some invisible solids that restrict movement of a sprite but I'm sure there's a better way.

    Any help would be appreciated.

  • I'm still looking to get some insight into how to create this limited movement for the player. Any help would be appreciated.

  • There are two addons you can try:

    https://www.construct.net/en/forum/extending-construct-2/work-in-progress-addons-30/behavior-rexspline-114539

    https://www.construct.net/en/forum/extending-construct-2/addons-29/behavior-splinepath-45373 (there is a working donwload link in comments)

    I actually was thinking of these, but I want to able to control the movement of the sprite manually. Essentially I want the player to move their character to the Left/Right of the screen to catch an object along a 'rail' like path. It's like how in the old game Warlords you can move your character in only an arc (which is the 'U' shape path I mentioned).

    The spline path thing, however, is just great for moving enemies around and I can't wait to implement it in something.

  • If you open Rex's demo project and add this code, you can make the sprite to move in reverse at any time:

    You can use this method to change direction. If you need, you can also slow the sprite down by decreasing speed. Say, if the sprite is moving to the right and player is holding Left key, decrease speed until it falls to 0. Then change direction and start increasing speed.

  • If you open Rex's demo project and add this code, you can make the sprite to move in reverse at any time:

    You can use this method to change direction. If you need, you can also slow the sprite down by decreasing speed. Say, if the sprite is moving to the right and player is holding Left key, decrease speed until it falls to 0. Then change direction and start increasing speed.

    Well, this turned out to be interesting.

    I added the code to one of the spline demos I have and it sometimes works. Then there's the whole issue of the object being generated by clicking START and then sometimes gets stuck on the waypoints. The reversing of direction sometimes works.

    This just isn't going to work for a player controlled object IMO. But I can use this for some bizarre enemy movements. Maybe what I need is to have the player stuck on a 'conveyor belt' type path that can have angles in it and move the player left/right that way.

  • The upside down T sounded interesting, which is basically a path that has branches. Here's what I came up with. Defining the paths is pretty tedious since you have to place a list of the uids of all the connected nodes in each node, but the events are fairly simple albeit dense.

    dropbox.com/s/maef5spnv5olc07/path_follow_2.capx

    The motion is driven by the the input angle. If the object is between nodes the object will move forward or backwards depending if the input direction is pointing more in one way or another. Also when a node is hit it will look at all the connecting nodes and pick the one with the lowest anglediff with the input angle to move towards.

    The events a mostly easy enough to follow I think. The exception is probably the formula in event 13:

    obj: add (cos(inputDir-self.ang)>0?1:-1)*speed*dt to d

    inputDir is the angle from the keyboard input

    self.ang is the angle between the current pair of nodes.

    cos(inputDir-self.ang) is the dot product between two unit vectors made from those angles. It just simplifies to that. If the dot product is positive then the inputDir is mostly in the same direction as the angle between the nodes. And if it's negative then it should go backwards.

    The conditional ...cos(inputDir-self.ang)>0?1:-1 is to make it only positive or negative 1. That is to keep the speed constant.

    The remainder is to apply the speed in that direction with dt.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • The upside down T sounded interesting, which is basically a path that has branches. Here's what I came up with. Defining the paths is pretty tedious since you have to place a list of the uids of all the connected nodes in each node, but the events are fairly simple albeit dense.

    https://www.dropbox.com/s/maef5spnv5olc07/path_follow_2.capx?dl=1

    The motion is driven by the the input angle. If the object is between nodes the object will move forward or backwards depending if the input direction is pointing more in one way or another. Also when a node is hit it will look at all the connecting nodes and pick the one with the lowest anglediff with the input angle to move towards.

    The events a mostly easy enough to follow I think. The exception is probably the formula in event 13:

    obj: add (cos(inputDir-self.ang)>0?1:-1)*speed*dt to d

    inputDir is the angle from the keyboard input

    self.ang is the angle between the current pair of nodes.

    cos(inputDir-self.ang) is the dot product between two unit vectors made from those angles. It just simplifies to that. If the dot product is positive then the inputDir is mostly in the same direction as the angle between the nodes. And if it's negative then it should go backwards.

    The conditional ...cos(inputDir-self.ang)>0?1:-1 is to make it only positive or negative 1. That is to keep the speed constant.

    The remainder is to apply the speed in that direction with dt.

    Holy Crap. This works like a dream. I wouldn't even begin to understand anything involving all that Construct math. I was able to adjust the speed of the object so that's a plus.

    How exactly would you be able to add more nodes? I couldn't figure out your number setup for the 'wide U shape' nodes. The setup for the 'wide U shape' seems to be the better one since it has the most nodes for the object to search for. I was even able to shape it like an 'upside down T' as well and have it so that the object moves all the way to the right with just pressing right on the keyboard (pressing left led to the object getting stuck until I pressed up).

    In my game, the player only has to press Left/Right and the sprite would move to the far end of the set path. If you can show how to add more nodes properly then you could make a path that goes just about anywhere in the layout in any shape.

    Thanks alot to the both of you.

  • The nodes have an instance variable “links”, which is a comma separated list of the Uids of the connected nodes.

    So say you wanted a path through four nodes with uids 2,5,3,6 you’d do this:

    Node 2 links = “5”

    Node 5 links = “2,3”

    Node 3 links = “5,6”

    Node 6 links = “3”

    If that makes sense.

    Since you just want to be able to move forward and backward on the path my capx isn’t quite suited for it. It can be made simpler but I’ll have to look at that later.

  • YoHoho I made a quick demo - roughly based on other path following samples made by ROJOhound years ago :)

    you can add as many nodes as you want - the only catch is you have to set the NodeNumber instance variable consecutively. if you have nodes 1,2,3,4,5,8 it will stop at node 5 because it wont find a node 6.

    you can arrange the nodes any way you want - it will move from one to the next (either up or down depending on the direction).

    https://www.rieperts.com/games/forum/follownodepath.capx

  • YoHoho I made a quick demo - roughly based on other path following samples made by ROJOhound years ago :)

    you can add as many nodes as you want - the only catch is you have to set the NodeNumber instance variable consecutively. if you have nodes 1,2,3,4,5,8 it will stop at node 5 because it wont find a node 6.

    you can arrange the nodes any way you want - it will move from one to the next (either up or down depending on the direction).

    https://www.rieperts.com/games/forum/follownodepath.capx

    Now this totally works the way I want it! Clearly I never would have figured any of this out on my own. Thanks again to all 3 of you for making an effort to create this.

  • Here's another example. With it you can either define the path with the order you create the nodes, or set the node.order variable to define the order and event 4 with recreate them in that order. They don't have to be sequential numbers, just as long as the next one is higher than the previous.

    dropbox.com/s/c2a2xy1yn78i0ql/path_follow_3.capx

    The con with this method is it only allows one path.

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