This enemy i made sometimes goes though collisions, how do I stop that?

Godot Version

4.2.2

Question

So I set this enemy to charge at the player. However it goes through walls in my tilemap. Here’s my code:

extends CharacterBody2D

var ACCELERATION = 1500
var FRICTION = 600
var SPEED = 1000.0
var SPEEDLESS = 200.0
var loopy = 3
var goofy = 1
var othergoofy = 0
var stop = true 
var dizzy = 0
var motion = false
var breaking = false
var motionstart = false
var motionend = false


@onready var player = get_parent().get_node("Player")
@onready var playerpos = global_position.direction_to(player.global_position)
@onready var rng = RandomNumberGenerator.new()
@onready var my_random_number = rng.randf_range(1, 100)
func _process(delta):
	
	pass

func _physics_process(delta):
	
	if motion == true:
		$AnimationPlayer.play("rollrollypoly")
	elif motionstart == true:
		$AnimationPlayer.play("startrollypoly")
	elif motionend == true:
		$AnimationPlayer.play("endrollypoly")
	elif breaking == true:
		$AnimationPlayer.play("dizzyrollypoly")

	else:
		

		$AnimationPlayer.play("idlerollypoly")
	if stop == false:
		motion = true
		global_position.x += (playerpos.x * SPEED) * delta
		global_position.y += (playerpos.y * SPEED) * delta
		await get_tree().create_timer(1).timeout
		
		
		motion = false
		motionend = true
		await get_tree().create_timer(0.45).timeout
		motionend = false
		stop = true
	elif stop == true and my_random_number < 2:
		playerpos = global_position.direction_to(player.global_position)
		

		breaking = true
		await get_tree().create_timer(1).timeout

		motionstart = true
		await get_tree().create_timer(0.45).timeout
		motionstart = false
		breaking = false
		stop = false
	else:
		playerpos = global_position.direction_to(player.global_position)
		

		
		await get_tree().create_timer(1).timeout

		motionstart = true
		await get_tree().create_timer(0.45).timeout
		motionstart = false
		
		stop = false
		
		pass


	
	move_and_slide()

these awaits timeout at process frame is pending the move_and_slide() calculation, hence sometimes it looks like the enemy can overlap with other stuff, because move_and_slide is not per frame called to check if it’s colliding

solution is either use Timer node to calculate time delay or use time variable with increment by delta and check if time hit X seconds, do something or the other way around

idk if you put

at the very first line of _physics_process(delta) would it work or not.
worth to try
but yeah, putting await in process function just doesnt look right

1 Like

Putting move and slide at the beggining of physics process did work. Thanks!
:smiley:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.