Projectile won't move

Godot Version

# projectile.

extends Area2D

const speed := 250.0

func _physics_process(delta):
	position.y += speed * delta
	

func _on_area_entered(area):
	pass

func _on_visible_on_screen_enabler_2d_screen_exited():
	queue_free()


# enemy.

extends Enemy_Target

var walk_speed
var target
var A_mode
var patrolMarkL : float
var patrolMarkR : float
var patrolMarkO : float
@export var proj : PackedScene

@onready var stun = $Stun
@onready var rayL = $RayL
@onready var rayR = $RayR
@onready var sightL = $LineofsightL
@onready var sightR = $LineofsightR


enum Modes {
	Patrol,
	Attack,
	Chase,
}

enum States {
	Still,
	Move,
	Drop
}


func _ready():
	walk_speed = 200.0
	speed = 275.0
	health = 10
	A_state = States.Move
	A_mode = Modes.Patrol
	patrolMarkL = position.x - 348
	patrolMarkR = position.x + 348
	patrolMarkO = position.x
	direction = -1
	target = get_tree().get_first_node_in_group("player")

func _physics_process(delta):
	if active == true:
		
		match A_mode:
			Modes.Patrol:
				if position.x >= patrolMarkR:
					direction = -1
				elif position.x <= patrolMarkL:
					direction = 1
				
				if sightL.get_overlapping_bodies() or sightR.get_overlapping_bodies():
					A_mode = Modes.Chase
					print(A_mode)
				
				if target.position.y > position.y and position.x - 25 < target.position.x and target.position.x < position.x + 25:
					A_mode = Modes.Attack
				
			Modes.Chase:
				if target.global_position.x > position.x:
					direction = 1
				else:
					direction = -1
				
				if !sightL.get_overlapping_bodies() and !sightR.get_overlapping_bodies():
					A_mode = Modes.Patrol
					print(A_mode)
				
			Modes.Attack:
				A_state = States.Still
				stun.start()
				var projectile = proj.instantiate()
				projectile.position = global_position
				
		
		match A_state:
			States.Still:
				velocity.x = 0
			States.Move:
				if A_mode == Modes.Chase:
					velocity.x = direction * speed
				elif A_mode == Modes.Patrol:
						velocity.x = direction * walk_speed
		
		move_and_slide()

func take_damage(damage):
	health -= damage
	A_state = States.Still
	A_mode = Modes.Chase
	checkPlayerPosition()
	stun.start()
	if health <= 0:
		queue_free()

func _on_visible_on_screen_enabler_2d_screen_entered():
	active = true

func _on_visible_on_screen_enabler_2d_screen_exited():
	queue_free()

func checkPlayerPosition():
	if target.global_position.x > position.x:
		direction = 1
	else:
		direction = -1

func _on_stun_timeout():
	A_state = States.Move

Question

My enemy is supposed to drop a projectile when the player walks under it, but the projectile just stays frozen in the air, and also the enemy gets stuck until the player walks into it's line of sight zone.

It seems you are not adding the projectile to the scene tree. So currently, the projectile should not just stand still, it shouldn’t appear in the game at all.

			Modes.Attack:
				A_state = States.Still
				stun.start()
				var projectile = proj.instantiate()
				projectile.position = global_position
				
				# depending on your scene tree setup, you might want
				# to parent it to a different node
				get_parent().add_child(projectile)

And it seems there is no way for the enemy to leave attack mode currently (exept if he takes damage), which also causes the enemy to constantly set his state to States.Still while restarting the stun timer. So you need to implement a way to switch out of Modes.Attack to your state machine.

1 Like

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