Godot Version
<v4.2.2>
Description
Hi guys I’m following a tutorial(https://www.youtube.com/watch?v=otHfaomtJh0&t=575s) on making a golem boss and i am stuck at 9:49 / 18:09 because my boss is not following the player
1)I have a CharacterBody2D named BossGolem that is equipped with the script that named BossGolem.gd and script is like this:
BossGolem.gd:
extends CharacterBody2D
@onready var player = get_parent().find_child(“Player”)
@onready var sprite = $Sprite2D
var direction : Vector2
func _ready():
set_physics_process(false)
func _process(_delta):
direction = player.position - position
if direction.x < 0:
sprite.flip_h = true
else:
sprite.flip_h = false
func _physics_process(delta):
velocity = direction.normalized() * 40
move_and_collide(velocity * delta)
- CharacterBody2D(BossGolem) has a child node of node2D named FiniteStateMachine that also has a few child node of node2D named Boss_Idle, Boss_Follow and Boss_MeleeAttack. Each child node of FiniteStateMachine is the state of the boss and each of them had attached a script named Boss_State.gd.
Boss_State.gd:
extends Node2D
class_name State
@onready var debug = owner.find_child(“Debug”)
@onready var player = owner.get_parent().find_child(“Player”)
@onready var animation_player = owner.find_child(“AnimationPlayer”)
func _ready():
set_physics_process(false)
func enter():
set_physics_process(true)
func exit():
set_physics_process(false)
func transition():
pass
func _physics_process(_delta):
transition()
debug.text = name
- For the Boss_Idle node(State), its script has extended with the script named Boss_Idle.gd and Boss_Follow node(State) is also extended with the script named Boss_Follow.gd.
Boss_Idle.gd:
extends State
@onready var collision = $“…/…/PlayerDetection/CollisionShape2D”
@onready var progress_bar = owner.find_child(“ProgressBar”)
Called when the node enters the scene tree for the first time.
var player_entered: bool = false:
set(value):
player_entered = value
collision.set_deferred(“disabled”, value)
progress_bar.set_deferred(“visible”,value)
func transition():
if player_entered:
get_parent().change_state(“Boss_Follow”)
print(“hui”)
func _on_player_detection_body_entered(_body):
player_entered = true
print(player_entered)
Boss_Follow.gd:
extends State
func enter():
super.enter()
owner.set_physics_process(true)
animation_player.play(“Boss_Idle”)
func exit():
super.exit()
owner.set_physics_process(false)
func transition():
var distance = owner.direction.length()
if distance < 30:
get_parent().change_state("Boss_MeleeAttack")
- The function in Boss_Idle.gd has a function named “_on_player_detection_body_entered” and it is to detected whether the player has entered the collisionShape of the CharacterBody2D(BossGolem). Furthermore, the function has been linked with a Child node of CharacterBody2D(BossGolem) that is an Area2D named PlayerDetection and the child node of PlayerDetection is a CollisionShape2D that is for detecting players.
5)CharacterBody2D(BossGolem) has its collision layer on Layer 2 and have no mask while Area2D(PlayerDetection) is on Layer 2 as well but no mask layer. As for the CharacterBody2D(Player), its collision Layer is at 2 and the mask is at 1.
My CharacterBody2D(BossGolem) node placement:
##Question
The reason I describe all of these to is to solve why my CharacterBody2D(BossGolem) could not change the Boss_Idle state to Boss_Follow stage but instead change to Boss_MeleeAttack when player is not even very close to it, the CharacterBody2D(BossGolem) could not move around the screen but stay static? Also my level and tilemap that has its collision layer and mask layer set at level 1 and 2.
Any help would be great, Thanks!