0 votes

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) 
Godot version 3.4.4 stable
in Engine by (73 points)

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.

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) 

Hmmm... Then I don't know what's happening.

1 Answer

0 votes

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?

by (283 points)

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

Thanks:)

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?

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

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.