我不知道如何使角色body2d转到godot中node2d的位置我的场景是一个游戏场景,其中包含一个玩家和一个关卡场景关卡场景包含一个tilemap和一个玩家所在的area2d(盒子)使用此脚本的 2d 角色身体:
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()
游戏场景有这样的脚本:
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
关卡有这个脚本:
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)
我尝试过使用聊天gpt,但他对Godot有点过时了,不起作用我也尝试自己做,但它有很多问题
我自己通过在播放器中查找 e 按下并检查到盒子的距离是否小于 60 来完成,然后我将布尔值设置为 true,当该布尔值为 true 时,我将盒子位置设置为玩家的位置并将其移近鼠标 40 像素,使其不在播放器内部: