戈多多人游戏。即使我正在发送数据,动画也不会在主机和客户端之间同步

Godot Multiplayer. Animations are not syncing between the host and client even though I am sending the data

我知道我在一天前做过类似的 post,但我只是 运行 遇到了另一个问题。我一直在用 Godot 制作 2 人多人游戏。当我尝试将动画与主机和客户端同步时,即使我将所有正确的信息发送给客户端,也没有任何反应。我将动画状态发送给客户端,然后客户端应该播放动画状态。

我真的不知道我能做些什么来解决这个问题。

Player.gd:

extends KinematicBody2D


const ACCELERAION = 512
const MAX_SPEED = 64
const FRICTION = 0.25
const GRAVIY = 200
const JUMP_FORCE = 128

var motion = Vector2.ZERO

var animationState = ""

onready var sprite = $Sprite
onready var animationPlayer = $AnimationPlayer

remote func _set_positon_and_animation_state(pos, hFlip, animState):
    global_transform.origin = pos
    sprite.flip_h = hFlip
    animationState = animState

func _physics_process(delta):
    var xInput = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    
    print(xInput)
    
    if xInput != 0:
        animationState = "Running"
        motion.x += xInput * ACCELERAION * delta
        motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
        if is_network_master():
            sprite.flip_h = xInput < 0
    else:
        animationState = "Standing"
    
    motion.y += GRAVIY * delta
    
    if is_on_floor():
        if xInput == 0:
            motion.x = lerp(motion.x, 0, FRICTION)
        if Input.is_action_just_pressed("ui_up"):
            motion.y = -JUMP_FORCE
    else:
        animationState = "Jumping"
        
        if Input.is_action_just_released("ui_up") and motion.y < -JUMP_FORCE / 2:
            motion.y = -JUMP_FORCE / 2
        if xInput == 0:
            motion.x = lerp(motion.x, 0, 0.2)
    if is_network_master():
        motion = move_and_slide(motion, Vector2.UP)
        rpc_unreliable("_set_positon_and_animation_state", global_transform.origin, sprite.flip_h, animationState)
    animationPlayer.play(animationState)

World.gd

extends Node2D

onready var playerSpawn = $SpawnPoint
onready var playerSpawn2 = $SpawnPoint2


func _ready():
    var player1 = preload("res://Player.tscn").instance()
    player1.set_name(str(get_tree().get_network_unique_id())) #Gives the host player a unique id when they join the server
    player1.set_network_master(get_tree().get_network_unique_id()) # Specifies this player owns a different character controller than the other player
    player1.set_global_transform(playerSpawn.global_transform)
    add_child(player1)
    
    var player2 = preload("res://Player.tscn").instance()
    player2.set_name(str(Globals.player2id)) #Gets the id the player got when they joined the game cant use the same function as player 1 because it will give us the same id and crash the game
    player2.set_network_master(Globals.player2id) # Specifies this player owns a different character controller than the other player
    player2.set_global_transform(playerSpawn2.global_transform)
    add_child(player2)

我哪里做错了,其他一切似乎都正常!

您对 _set_positon_and_animation_state 进行 RPC 传递 animationState:

rpc_unreliable("_set_positon_and_animation_state", global_transform.origin, sprite.flip_h, animationState)

然后在 _set_positon_and_animation_state 的代码中,您在 animState 参数中使用 animationState 参数,并用它设置 animationState 字段:

remote func _set_positon_and_animation_state(pos, hFlip, animState):
    global_transform.origin = pos
    sprite.flip_h = hFlip
    animationState = animState

但是您没有使用 animationState 的那个值。相反,我看到的是你在调用 play:

之前在 _physics_process 中覆盖它(取决于输入)
    animationPlayer.play(animationState)

这也引出了您将如何处理输入的问题。


我想你会想在 _set_positon_and_animation_state 中调用 play:

remote func _set_positon_and_animation_state(pos, hFlip, animState):
    global_transform.origin = pos
    sprite.flip_h = hFlip
    # animationState = animState
    animationPlayer.play(animState)

在我看来同步动画也意味着计时。如果你想处理这个问题,由于网络的原因会有一些延迟。但是先播放动画。