Godot Version
`Godot 4.4
Question
I have a variable set to true if the character comes into contact with a tile in tile map. Right now, it only works if the character comes in from the top or, from current testing, bottom. But, I would like for it to set true no matter what side is contacted. Is there any way to do this?
Thank you for reading.
What code are you using? Please post it here.
Thanks for responding. I don’t have much for the logic, just have the layer of tiles that make up the world set in a group called walls, then a var touch_wall = false in character script as well as a body_area_2d_is_entered function connected in the character script and I have
touch_wall = true
as the logic in the function.
It returns true when the area touches the top and actually the bottom from my most recent attempts, but I get nothing from the sides.
What signal is the body_area_2d_is_entered
function connected to?
Sorry I am new, I was thinking that was the signal, it’s the _on_area_2d_body_entered function in the characterbody script I have, and then I have and _on_area_2d_body_exited one as well. I have one more tilemap video I am going to try and if that doesn’t show me the solution ( I was kind of thinking something with setting up terrain: terrain isn’t the answer
) then I might go the route of attempting to code it directly and figure out if the characterbody is in the tile directly to the right or left of that used tile and if so and is_colliding or something, then set the variable to true. I really like the godot interface, so I am hoping to utilize that for the functionality, I love programming but I just really have a hard time wrapping my head around it and how to do things properly. Even when trying to look into it I just can’t see why all the pieces are fitting together how they do. Love the process and definitely glad to be able to access this community of people wanting to help, but man each next step can feel like a mile high. Appreciate your help
I think Raycasts are the solution. Since the geometry is square I think I just need to make it go in 4 directions and then if one of those 4 is on base then it will switch on the touched variable, I just had this idea so I will come back and update if I totally failed again or not
Are you using a CharacterBody2D for the player? If you are, you can use get_slide_collision()
to get the information on what the character has collided with when you call move_and_slide()
.
1 Like
Yes I am that sounds very promising and havent tried it, I am not sure how I should implement it.
You are awesome
I put it like this, in my physics process and it worked. Now, only issue is it sticks and won’t let me move it, going to hammer on that one next. But, I appreciate your help. If you have any ideas for how to unstick and move and then be able to restick again I am all ears
this is what I have to make it work for sticking
var collision_count = get_slide_collision_count()
if collision_count > 0:
for i in range(collision_count):
var collision = get_slide_collision(i)
if collision:
touch_wall = true
I know I have to then set it to false and then true again, but I am trying to work out the logic and my head is spinning lol omg I am a goof so I think the solution is so simple. Idk how many hours lol thank you for helping me. Your logic managed to get me to the solution: what I did was set the characters location at the time it touches the wall and then before the logic in this comment put an if prev_pos != self.global_position…if you have a better way to do this I would love to learn it but until I hear otherwise I’m gonna roll with this.
Thanks again
1 Like