Useing raycast for a movement system

I am trying to make a top down rpg game. I have a specific way i want the movement to work

I want to minimize the amount of objects i need to use in a scene and want the movement to be grid-locked. my idea is that the player would cast a ray in 8 directions anf when you press one of the keys, it would check the value in front of you to to get a number.

1: there isn’t anything there you can move

2: there is something in the way, you cannot move

3: there is something you can interact with, you can move

4:a cut-seen triggers
So forth and so on, the problen is that how will i actualy store all of that data?

Hey there,
This question is tied to how you plan to implement other parts of the game as well. Will it use tile map as background? Or will it use a static sprite image?

One way of doing it is with 3D arrays, I assume you know how they work.

For walls, you can just check if the player is colliding with wall tiles, and use “Serparate objects” action. (It might look jittery - to avoid that you can use an invisible Sprite to check ahead of the direction the player will move - if this Sprite is not colliding with wall tile, do movement event)

let’s say there are 2 events based on X and Y coordinates of the grid, and event type:
array1a : 11, 30, 4
array1b: 9, 15, 3
array1c: 9, 16, 3
after player finishes walking, check if any of their coordinates match the array, and do something based on its condition.

however, working with 3D arrays in Gdevelop is not something I’ve done, if it’s even possible. also, check out the tutorial projects section to see if anything is close to what you want!

Here’s a way that uses point is inside. It’s not smooth with multiple presses because it uses a tween but it could be modified.

try me it uses the arrow keys

source: (click green [code] and select [download zip]

2 objects a player and a wall, The player object has 2 object number variables newX and newY and an object boolean of moving.
It uses a grid size of 32,32 The player origin point is at 0,0 So, it adds 16 to the x,y to find the cent point.

edit: moving the tween is finished event to the top makes it move a little smoother.

The nice thing is you can control the speed by changing the tween duration using a variable.

1 Like

I tweaked this to add a run mode when control left is pressed. Also, I added so if the player can’t move diagonally, it will move in the direction that it can move. Meaning, if the up and left is pressed but it’s blocked above then it moves to the left. I’m a bit surprised how easy this was to add.

Try me, uses arrow keys plus left control for run

I created a separate project

I decreased the zoom to make it fit one screenshot but it should still be readable. If not, the source is on GitHub.

1 Like

This is almost perfect, i just need to add a way to check what direction the player is facing and if they are behind or in front of something. Your A life saver dude.

P.S. how do i actually but the code you made into my project?

1 Like

You’re welcome. You can manually type it or if you can open 2 copies of the app, you should be able to highlight, copy and paste all of the events. You would need to change the events to match your project and add the necessary variables.

I guess the direction would be the angle from the objects position right before the tween and the value for x, y used in the tweens.

Instead of the wall object, you could use a group name with all of the obstacles in it instead. You could also use a point is inside without the not to test for an object and compare the group objects name. That would only use the original newX and newY since you want to know what is directly in front of the player. Or it could be based off of the players position and direction it’s facing.

If point is inside group
Compare 2 strings group object name is name

Screen Shot 2024-02-10 at 6.53.55 PM
hey, the thing works now ,but the hittbox effets the top of the sprite, do you know how i can make it do so for the bottem instead

Edit: I changed my strategy. While this works. It doesn’t allow for sliding along a surface if the player tries to move diagonally. I wonder if you were to tween the x and y separately and then cancel the directions separately. I’m not sure how that would work. IDK. I like the collision idea.

The other method was too strict. To check for a tall shape, you’d have to either move the points or use multiple point is inside checks. One for the top and 1 for the bottom. Or us a rectangular grid.

I was going to try to rework the math but what if we just change the method? Instead of checking if you can move, use a collision to cancel the move and use another tween to move back to the previous location. Sort of like separate objects but locked to the grid.

Try me. I added some more obstacles.


I added the object variables lastX and lastY. You may want to shrink the player mask so there’s a little wiggle room. The mask should be smaller than the grid size. meaning if the grid is 32x32 then the object should be like IDK 30x30 or smaller or 30x62. Otherwise, you won’t be able to get right against the other objects. I set the collision detection to ignore objects that are touching each other.

Something like this:

Maybe this example can help: