Problem with is_on_wall() function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TopBat69

Hey

I making a endless 2d runner in the godot engine. All the mechanics work fine except the enemy one(you have to dodge them in order to survive).Basically I want to flip my enemy(like the sprite and the direction).The code below is the enemy script. it flips the character when needed. The one that detects cliffs using raycast works fine but whenever the enemy is on a wall if flips left and right simultaneously. Could anyone help me with that? Thank You

extends KinematicBody2D

export var direction : int = 1
var speed : int = 35

var velocity : Vector2 = Vector2.ZERO

func _ready():
	$AnimatedSprite.play("idle")
	if direction == -1:
		$AnimatedSprite.flip_h = true
	$RayCast2D.position.x = $CollisionShape2D.shape.get_extents().x * direction	
		
func _physics_process(delta):
	velocity.y += 20
	if is_on_floor():
		velocity.x = speed * direction
		
	
	if is_on_wall() or not $RayCast2D.is_colliding():
		direction *= -1
		$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h
		$RayCast2D.position.x = $CollisionShape2D.shape.get_extents().x * direction	

	velocity = move_and_slide(velocity, Vector2.UP)	

It could be because its also touching the floor. This could occur sense the wall is flipping it at the same time as the floor. The solution to that would be to check if its on the floor and the wall at the same time and if it is only listen to one of them.

toivocat | 2022-05-13 16:45

I did what you said. I checked if its on the floor as well, but the problem still occurs
Code:

extends KinematicBody2D

export var direction : int = 1
var speed : int = 35

var velocity : Vector2 = Vector2.ZERO

func _ready():
	$AnimatedSprite.play("idle")
	if direction == -1:
		$AnimatedSprite.flip_h = true
	$RayCast2D.position.x = $CollisionShape2D.shape.get_extents().x * direction	
		
func _physics_process(delta):
	velocity.y += 20
	if is_on_floor():
		velocity.x = speed * direction
		
	
	if (is_on_wall() or not $RayCast2D.is_colliding()) and is_on_floor():
		direction *= -1
		$AnimatedSprite.flip_h = not $AnimatedSprite.flip_h
		$RayCast2D.position.x = $CollisionShape2D.shape.get_extents().x * direction	

	velocity = move_and_slide(velocity, Vector2.UP)	
	

TopBat69 | 2022-05-14 06:48

Hmmm… Then I don’t know what’s happening.

toivocat | 2022-05-14 14:08

:bust_in_silhouette: Reply From: ichster

Check to see what is causing the first and second flip.

There are two sources, is_on_wall() and not $RayCast2D.is_colliding(), that could cause a flip. Is it possible that as soon as the first flip source triggers, the second immediately triggers again?

I understood what you mean, but can you give a simple solution please

Thanks:)

TopBat69 | 2022-05-14 03:24

Sure I can give a code example. But first, I am a little unsure about what $RayCast2D is for? Is it for detecting cliffs/walls?

ichster | 2022-05-16 19:15

I have actually fixed the problem by creating a boolean called flipped and when the enemy is on a wall flipped is set to its opposite Value. I have coded what should happen when it is flipped or not.BTW the raycast was for detecting cliffs its neither too short nor too long

TopBat69 | 2022-05-17 03:54