#Spawning Problem with a PET on my 2D game

1 messages · Page 1 of 1 (latest)

slender urchin
#

Hi guys, I'm making my 2d platformer and I just started making the pet. The problem is that the pet spawns where I dont want to (on the tree) and also it doesnt chase the player in the move state even if the animation is playing

#

PET STATE MACHINE:

extends Node
class_name PetStateMachine
@export var initial_state: PetState
var current_state: PetState
@export var idle: PetState 
@export var move: PetState



func init(parent: CharacterBody2D):
    for child in get_children():
        child.parent = parent
    change_state(initial_state)

func process_input(event: InputEvent):
    var new_state = current_state.process_input(event)
    if new_state:
        change_state(new_state)

func process_physics(delta: float):
    if current_state == null:
        print("No current state set!")
        return
    var new_state = current_state.process_physics(delta)
    # print(current_state)
    if new_state:
        print("State changing to: ", new_state)
        change_state(new_state)

func process_frame(delta: float):
    var new_state = current_state.process_frame(delta)
    if new_state:
        change_state(new_state)

func change_state(new_state: PetState) -> void:
    if new_state == current_state or new_state == null:
        return
    if current_state:
        current_state.exit()
    print("State transition %s => %s" % [current_state, new_state])
    current_state = new_state
    current_state.enter()

#

PET IDLE

#
extends PetState
@onready var move: PetState = $"../Move"

func enter():
    print("Rumblerat IDLE")
    parent.animation_player.play("idle")

func process_physics(delta: float) -> PetState:
    if parent.velocity.length() > 0.1:
        return move
    return null

#

PET MOVE

#
extends PetState

@export var follow_offset := Vector2(-40, -30)
@onready var idle: PetState = $"../Idle"
@onready var pet_state_machine: PetStateMachine = $".."

func enter():
    print("Pet in MOVE")
    parent.animation_player.play("move")
func process_physics(delta: float) -> PetState:
    if !parent.is_collected:
        return null  

    var player = parent.target_player

    if player == null:
        print("Player non trovato")
        return null

    if player.velocity.length() <= 0.1:
        return idle

    var target_pos = player.global_position + follow_offset
    var current_pos = parent.global_position

    var direction = (target_pos - current_pos).normalized()
    print("VELOCITY MOVING")
    parent.velocity = parent.velocity.lerp(direction * move_speed, delta * 10.0)
    parent.move_and_slide()

    return null

slender urchin
#

<@&1349109171240439858>