对于 godot,我正在尝试生成子弹,子弹生成工作正常,但我不知道如何将其联网,我尝试将其生成为玩家的兄弟姐妹和孩子,但我无法让它显示在所有内容中客户。
玩家代码:
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 节点。
就您而言,这很简单