+1 vote

I have a kinematicbody2D walking randomly in 4 directions (up, right, down, left) and I want that when the kinematicbody2d collides with a wall - that is made by tilemap - it can recognize the collision, to change the direction of random move.

I was think the **is_on_wall** method was used to do this, but I didn't understand the documentation. Am I right and **is_on_wall** will serves to me or it have another way to do the collision recognition between kinematicbody2d with a wall?

in Engine by (174 points)

1 Answer

0 votes
Best answer

In templates, take a look at "2D Platformer demo" and open the enemy.tscn. The enemy uses 4 Raycast2D do detect walls and floor, making a movent back and forth over a platform without fall:

if not detect_floor_left.is_colliding() or detect_wall_left.is_colliding():
    direction = 1.0

if not detect_floor_right.is_colliding() or detect_wall_right.is_colliding():
    direction = -1.0
by (91 points)
selected by

made with your tip and is working perfectly.

thanks

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.