我正在尝试从播放器中生成子弹,子弹生成工作正常,但我不知道如何将其联网。我尝试将它生成为玩家的兄弟姐妹和孩子,但我无法让它显示在所有客户端上。
玩家脚本:
extends CharacterBody2D
var health = 10
func _enter_tree():
set_multiplayer_authority(str(name).to_int())
@onready var camera = %Camera2D
const SPEED = 330.0
const JUMP_VELOCITY = -400.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
if not is_multiplayer_authority(): camera.enabled = false
if is_multiplayer_authority():
if Input.is_action_pressed("use_shoot"):
shoot()
#if not is_multiplayer_authority(): return
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if is_multiplayer_authority():
if Input.is_action_pressed("i_up") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("i_left", "i_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func take_damage(amount):
print("YOUCH")
print("YOUCH")
health -= amount
if health <= 0:
queue_free()
@rpc()
func shoot():
var bullet = preload("res://scenes/bullet.tscn").instantiate()
add_child(bullet)
bullet.rotation = bullet.global_position.angle_to_point(get_global_mouse_position())
bullet.rotation_degrees += -90
func _on_area_2d_body_entered(body):
if body.is_in_group("Arbullet"):
queue_free()
项目符号脚本:
extends Area2D
var speed = -560
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
position += transform.x * speed * delta
func _on_body_entered(_body):
#if body.is_in_group("player"):
#body.take_damage(1)
#queue_free()
pass
我无法找出一种合理的方法来产生子弹而不伤害玩家。
从玩家的角度看子弹产生的样子:
这是如果我尝试添加同步器,但它们仍然不起作用,我已确保同步位置和旋转,谢谢。
(我认为所有错误都存在,因为我无法在玩家场景之外生成子弹。)
为了在网络上共享实例化场景,需要将这些场景添加到 MultiplayerSpawner 节点。
就您而言,这很简单
我刚刚注意到的一件事是,您从未设置实例化子弹的位置,因此您的子弹可能是在世界原点生成的,这就是您看不到它们的原因。
我喜欢解决这个问题的一种方法是创建一个 Marker2D 节点并将其移动到枪的末端/无论你用来射击的东西。然后在您的 Player 脚本中您将获得对此 Marker2D 的引用,并在实例化子弹后,您调用:
bullet.global_position = bullet_spawn_marker.global_position