The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+1 vote

well, i want to detect when there is a precipice to change de direction of the NPC so it doesn't fall, i have this but it doesn't work, i reed something about the test move but i donk can implement well yet so i use the isonfloor:

extends KinematicBody2D

const suelo = Vector2(0, -1);
const GRAVITY = 20;
const walk_speed = 75;

var movement = Vector2();
var left = Vector2(-1, 0);
var right = Vector2(1, 0);
var direction = right;

func _physics_process(_delta):
    movement.y += GRAVITY;
    movement.x = direction.x * walk_speed;
    movement = move_and_slide(movement, suelo);

#comprobamos cuando chaca con una pared para continuar el movimiento en direccion
    if is_on_wall():
        if direction == left:
            direction = right;
        elif direction == right:
            direction = left;

#ahora le damos la deteccion de caida, donde no hay suelo
    if self.is_on_floor():
        print("on floor");
    elif direction == right:
        direction = right
        direction = left
in Engine by (22 points)

Can you explain what happens, aside from it not working? At first glance this code seems like it would work to me.

The only thing that jumps out is that you are adding 20 to GRAVITY every frame, which eventually will be big enough that you will move through the floor without colliding with it.

what happent is that when there isn't more floor the NPC just fall and doesn't change the direction to keep walking and dont fall, idk if i have to do whit another funtion or more variables to the movement, and when i just print something in console when it's falling it's work properly, but when i change the direction nothing happend

I see. So I believe the problem is that by the time you detect that you have to turn around, you're already falling off of the platform?

I think this might be hard to work around with isonfloor().

I would suggest then looking at Raycasting.

You could setup a ray so that it will detect when you're about to fall off and turn around before you do.

Here is a demonstration of what I mean:

yeah, it's working properly whit ray casting, thanks for the suggestion

1 Answer

0 votes

I don't know if your problem is solved or not, but I suggest deleting the varaiables called: "left" and "right" and when you have to turn around you could do this instead of if statements: (It will have the same effect, but it's a lot more clean code-vise)

var direction = Vector2(1,0)
if is_on_wall():
   direction *= -1
by (137 points)
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.