|Attention||Topic was automatically imported from the old Question2Answer platform.|
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 is_on_floor:
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 #contraria 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 else: direction = left
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.
Ben Humphries | 2020-06-23 14:12
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
dm73994 | 2020-06-23 14:28
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 is_on_floor().
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: https://imgur.com/a/IhrAbAJ
Ben Humphries | 2020-06-23 14:47
yeah, it’s working properly whit ray casting, thanks for the suggestion
dm73994 | 2020-06-23 18:57