gpt4 book ai didi

c# - 统一网络 : Flipping sprites in Multiplayer doesn't flip host

转载 作者:行者123 更新时间:2023-11-30 23:32:15 28 4
gpt4 key购买 nike

我正在开发一款小型多人 2d 平台游戏。我已经设法让它在网络上复制动画、移动和翻转 Sprite ,但我有一个我无法理解的错误。翻转 Sprite 的代码非常适合主持人——他可以看到其他玩家在左转或右转时换边,但其他人看不到主持人翻转。第三个玩家也看不到第二个玩家的转弯等。有些地方不对,如果有人可以看一下我的代码,那就太好了。

主要播放器脚本中的代码:

if (input.x > 0 && !facingRight)
{
FlipSprite();
GetComponent<Player_SyncPosition>().FlipSprite();
}
else if (input.x < 0 && facingRight)
{
FlipSprite();
GetComponent<Player_SyncPosition>().FlipSprite();
}

在播放器的附加 Player_SyncPosition 脚本中编写代码:

 [ClientCallback]
public void FlipSprite()
{
if (isLocalPlayer)
{
CmdFlip();
}
}
[Command]
void CmdFlip()
{
if (!isLocalPlayer)
{
print("Switching sides");
facingRight = !facingRight;
Vector3 SpriteScale = GetComponent<Transform>().localScale;
SpriteScale.x *= -1;
GetComponent<Transform>().localScale = SpriteScale;
}
}

编辑:

播放器

Player_SyncPosition pSync;
void Awake()
{
pSync = GetComponent<Player_SyncPosition>();
}

void Update ()
{
if ((input.x > 0 && !pSync.facingRight) || (input.x < 0 && pSync.facingRight))
{
pSync.FlipSprite();
}
}

Player_SyncPosition

[SyncVar(hook = "FaceDirCallback")]
public bool facingRight = true;

[ClientCallback]
public void FlipSprite()
{
if (isLocalPlayer)
{
CmdFlip();
}
}
[Command]
void CmdFlip()
{
print("Switching sides");
facingRight = !facingRight;
Vector3 SpriteScale = transform.localScale;
SpriteScale.x *= -1;
transform.localScale = SpriteScale;
}

void FaceDirCallback(bool newValue)
{
print(facingRight);
print(newValue);
facingRight = newValue;
}

最佳答案

我遇到了各种各样的不同问题(向后移动等),这是有效的:

玩家:

void Awake()
{
syncPos = GetComponent<Player_SyncPosition>();
}

void Update()
{
if ((input.x > 0 && !facingRight) || (input.x < 0 && facingRight))
{
facingRight = !facingRight;
syncPos.CmdFlipSprite(facingRight);
}
}

Player_SyncPosition:

 [SyncVar(hook = "FacingCallback")]
public bool netFacingRight = true;

[Command]
public void CmdFlipSprite(bool facing)
{
netFacingRight = facing;
if (netFacingRight)
{
Vector3 SpriteScale = transform.localScale;
SpriteScale.x = 1;
transform.localScale = SpriteScale;
}
else
{
Vector3 SpriteScale = transform.localScale;
SpriteScale.x = -1;
transform.localScale = SpriteScale;
}
}

void FacingCallback(bool facing)
{
netFacingRight = facing;
if (netFacingRight)
{
Vector3 SpriteScale = transform.localScale;
SpriteScale.x = 1;
transform.localScale = SpriteScale;
}
else
{
Vector3 SpriteScale = transform.localScale;
SpriteScale.x = -1;
transform.localScale = SpriteScale;
}
}

关于c# - 统一网络 : Flipping sprites in Multiplayer doesn't flip host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34355894/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com