How do I…
I have a character with the Pathfinding behavior and a topdown perspective. The player should be able to click along the floor and the character will move to that location. The player should also be able to click on the walls and have the character move towards that location.
What is the expected result
If the player clicks on a wall, the character will move as close as it can get to that location on the floor without going onto the walls. It will stop when it reaches the wall.
What is the actual result
When the player clicks on a wall, the character will walk towards the wall, but once it reaches there, it may completely disappear, or it may suddenly teleport to the middle of the room and then walk towards the clicked location. I tried playing around with the collision masks to try counteract this, but it didn’t work.
Clicking the wall at the top usually works fine, but clicking the walls on the side causes it to act strangely.
If the pathfinder behavior is still trying to move the sprite while the separate objects is being triggered then they’re going to interact with each other. You need to stop the pathfinder behavior first. It’s going to be tricky especially when near the collision object.
General note: Pathfinding obstacles do not care about hitboxes. It uses the entire square of the object for valid movement.
There is an extension in the main extension list that may make this smoother, but even then I still believe it takes account the entire boundary box of the object as a pathfinding obstacle.
What I’d recommend is to invert what you’re doing. Set up your hitbox as “Walkable area” instead. Then use “Point is inside object” as a condition, make the object your room, and make the points the Mouse X and Y position expressions. That way it checks that the current destination (mouse cursor) is in the walkable area.
Then you use the “Move to” action you have already.
Thank you! Silver-Streak’s answer worked well. I was wondering if there was a way to make it so if you clicked on the wall, the character would move as close as it could to the wall without leaving the walkable area.
I’m curious what the end goal is. I’m guessing adventure or eacape room. Also, I’m guessing there will eventually be objects in the room. Are the objects going to be pathfinder obstacles?
The more info, the better the answers and the fewer rewrites youll need. Yes, I said fewer. Maybe, by a couple.
I was trying to make an adventure game and yes, there were objects on the floor of the room that were going to be pathfinder obstacles. I was going to check if the mouse was inside the object, use the code in the original post where I check for collisions, and manually write the coordinates for where the player will move to. It works alright, but it does have an issue of not moving the player if they click near the object but not on it.
I wasn’t sure how to approach any objects that are on the walls though.
I love the old school point and click games. You could make the collision mask larger even if it meant adding some transparent pixels around the actual drawing.
I kind of like the idea of putting all of the objects in a group and then picking the object closest to the mouse. You could even check the distance to limit it to say 20 pixels. The points would have to be in the center to make it consistent around the object.
Maybe a mixture of both with the point inside taking priority and the distance as a backup.
You could set a custom point for each object. That way if you click an object using a group you just need one check to get the objects x,y point for the destination.
You could add a slight outline or glow effect to the active object.
As far as clicking the wall. I’m sure there’s some combination of a ray cast either from the player or from the mouse x,y. You can get the x,y from where the ray collides with the object that you’re using as a floor hit box and use the x,y as a destination for the pathfinder.
Thank you for your suggestion on adding objects to a group and using the object’s point for the destination! That worked out really well.
However, I was having trouble figuring out how to get the raycast to work. This is what I have so far but it doesn’t work as intended. It’s under a condition that the left mouse button must be clicked.
I’m not at my pc but i believe you might want to cast from mouse to player targeting the sprite you’re using to keep the player in the room.
Oh, I see! Thank you. Is there a way to account for if the player is close to the wall? Like if you click on the wall, the player will walk there. But if you click towards the left on the same wall, the player won’t move that far as they are restricted to the sprite that keeps the player in the room.
I think the ray cast idea might work. I’m just saying do it from the mouse x,y to the player x,y against what I believe is named BG… If that’s the background sprite. You then use the pathfinder to go the the floorwall x&y like you have it.
I also thought, if the walls have painting, a window, a safe or even a door then the object would also contain the location or used as a reference to be used as the pathfinder destination.
It’s tough to imagine without the walls. Walls will give you a better idea of the concept. You don’t want to move beyond the wall but to a spot down from the point you click to the perspective line where the wall meets the floor and then away from the wall a foot or so.
Now, I’m wondering if the better method for the side walls anyways (maybe the front too) would be to cast a ray from the mouse x,y downward (90°?) against the floor (BG) sprite and then add or subtract to the returned values in the X direction.
The isometric example use another path finding behavior that handles hit-boxes but it’s experimental.
The 90° raycast works for the top wall, but I’m not sure how to add or subtract the returned values in the X direction for the side walls. The only thing I could think of was to have another check specifically for if the mouse was clicking on the side walls and using 0° there
I’m not positive if I know the problem but if it’s whether it’s left or right, a raycast or 2 would work. Cast one from 0, 90 and 180. If just 90 it’s down 0 and 90 equals left, 180 and 90 equals right.
You could also compare the mouseX to the BG.centerX. MouseX Less than bg.CenterX() it’s on left, greater than, it’s on the right. Mix in some Y values if needed.
I guess you could also add a separate bg sprite on the left and right maybe even top. It’s up to you.Then use point x,y is inside as a test. You can still cast down as long as the other sprites are different objects not just instances.
The sides wouldn’t even have to match. Just overlap on the side to the narrowist top corners.
If mouse x,y inside floor (BG) its floor
If not on floor but in left bg sprite then left
If not in floor but in right then right.
If it was a resource intense game then I’d prefer to use math but simple is also good.
Sorry for the late reply. I was trying to do it in the math way, but if I can’t work it out, I’ll do it the second way you recommended. I’m trying to do the raycast thing you suggested, but I’m not quite sure how to get it to work. With this code, when I start the game and click on the left, it will walk all the way to the right wall. It works when I click on the walls, but it goes in the opposite direction when I click on the floor.
I’d like to play around with the concept. I’ll get back to you.
This was fun. Here’s what I made while doing testing. I decided to add a second “floor” sprite to use to keep the player from going through the wall. I have no idea what you plan on doing. You might need it. I also added 2 sprites as reference to the top left and right corners of the room.
This is one way. It might not be the best way.
Thank you! I’ll try to investigate it more.