erthdev
1
Godot Version
Godot 4.7.2.stable
Question
I want to create a similar enemy as a gomba in my game but i dont know how to make him move the opposite way when colliding with a collision block?
extends CharacterBody2D
@onready var Anim = $AnimatedSprite2D
@onready var Collision = $CollisionShape2D
var killed = false
func _process(delta: float) -> void:
if killed == true:
Collision.disabled = true
else:
Collision.disabled = false
global_position.x += 0.7
func _on_head_collision_body_entered(body: Node2D) -> void:
if body.is_in_group("player"):
killed = true
Anim.play("Squished")
await get_tree().create_timer(0.3).timeout
queue_free()
Can you post what you found out. I was just about to reply to you. What solution did you use? It might also help anyone else with the same issue?
erthdev
4
extends CharacterBody2D
@onready var Anim = $AnimatedSprite2D
@onready var Collision = $CollisionShape2D
@onready var WallRayCast = $RayCast2D
const SPEED = 70.0
var killed = false
var direction := 1
func _physics_process(delta: float) -> void:
if killed:
Collision.disabled = true
velocity.x = 0
return
Collision.disabled = false
velocity.x = direction * SPEED
WallRayCast.target_position.x = direction * 10
if WallRayCast.is_colliding():
direction *= -1
WallRayCast.target_position.x = direction * 10
Anim.flip_h = direction < 0
move_and_slide()
func _on_head_collision_body_entered(body: Node2D) -> void:
if body.is_in_group("player") and not killed:
killed = true
Anim.play("Squished")
await get_tree().create_timer(0.3).timeout
queue_free()