I want to make a cube that i can pick up in godot

60 Views Asked by At

enter image description here

I have no idea how to make a character body2d go to the position of a node2d in godot my scenes are one gameplay scene which contains a player and a level scene the level scene contains a tilemap and a area2d(the box) the player is a character body 2d whit this script:

extends CharacterBody2D

@export var speed: float = 100


func _ready():
    pass

func _physics_process(_delta):
    var input: Vector2 = Vector2(
        Input.get_action_strength("right") - Input.get_action_strength("left"),
        Input.get_action_strength("down") - Input.get_action_strength("up"))

    # set velocity
    if Input.get_action_strength("run") == 1:
        velocity = input * speed * 1.8
    else:
        velocity = input * speed

    # move and slide
    move_and_slide()


the gameplay scene has this script:

extends Node2D

var bullet_scene = preload("res://scenes/Bullet/bullet.tscn")  # Adjust the path as needed
var canshoot: bool = true
@onready var shoot_timer: Timer = $"Player/Gun/Shoot Timer"

@onready var gun_position: Node2D = $Player/Gun

func _ready():
    pass

func _process(_delta):
        if Input.is_action_pressed("Primary_Mouse") and canshoot:
            # Create an instance of the Bullet scene
            var bullet_instance = bullet_scene.instantiate()
            # Set the starting position for the bullet
            bullet_instance.start_Position = gun_position
            # Add the bullet to the scene
            bullet_instance.name = "Bullet"
            $Player/Bullets.add_child(bullet_instance)
            shoot_timer.start()
            canshoot = false
            
func _on_shoot_timer_timeout():
    canshoot = true


and the level has this script:

extends TileMap

signal pick_up_box
signal pickup_enter

signal pickup_exit

func _on_activate_buton_body_entered(_body):
    set_layer_enabled(2, false)
    set_layer_enabled(3, false)
    print("done")
    
    


func _on_activate_buton_body_exited(_body):
    set_layer_enabled(2, true)
    set_layer_enabled(3, true)



I have tried using chat gpt but he is a little outdated with Godot and did not work I also tried doing it myself but it was very buggy

1

There are 1 best solutions below

0
Boostgb On

I did it myself by finding e presses in the player and checking if the distance to the box was less than 60 then I set a bool value to true and when that bool value was true I set the box position to the position of the player and moved it 40 pixels closer to the mouse so it wasn't inside the player: