Unity/Pun2: 2个玩家互相控制
Unity/Pun2: 2 players control each other
我只在一个团队中移动 2 人或更多人时遇到问题。在一个团队中,它应该运行,每个人都控制自己,但在另一个团队中,如果超过 1 个玩家加入,那么 1 个控制另一个。
我有 4 种玩家类型(2 个用于一个团队,2 个用于另一个团队),所以当我需要它们时,当我开始房间时,我会调用 4 个预制件。
void CreateController()
{
.
.
.
if (attackTeam == true && deffTeam == false && attAR_ == true)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Att_player_AR"), spawnpointA.position, spawnpointA.rotation, 0, new object[] { PV.ViewID });
if (attackTeam == true && deffTeam == false && attSniper_ == true)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Att_player_Sniper"), spawnpointC.position, spawnpointC.rotation, 0, new object[] { PV.ViewID });
if (deffTeam == true && attackTeam == false && defAR_ == true)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Def_player_AR"), spawnpointD.position, spawnpointD.rotation, 0, new object[] { PV.ViewID });
if (deffTeam == true && attackTeam == false && defSniper_ == true)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Def_player_Sniper"), spawnpointB.position, spawnpointB.rotation, 0, new object[] { PV.ViewID });
.
.
.
}
而且到处都应该检查 photonView 是否是我的
PhotonView PV;
.
.
.
void Awake()
{
PV = GetComponent<PhotonView>();
}
void Start()
{
if (PV.IsMine)
{
CreateController();
}
}
在这 4 个预制件中,我为所有这些都配备了 PhotonView,在脚本中,我检查 photonView 是否也是我的
protected virtual void Update()
{
if (!PV.IsMine)
{
return;
}
if (isDead == false && timerOut == false)
{
if (m_characterController && gamePaused == false)
{
// Check if Grounded,Wall etc
CheckIfGrounded();
CheckIfWall();
// Apply Smoothing
SmoothInput();
SmoothSpeed();
SmoothDir();
if (experimental)
SmoothInputMagnitude();
// Calculate Movement
CalculateMovementDirection();
CalculateSpeed();
CalculateFinalMovement();
// Handle Player Movement, Gravity, Jump, Crouch etc.
HandleCrouch();
HandleHeadBob();
HandleRunFOV();
HandleCameraSway();
HandleLanding();
ApplyMovement();
.
.
. }
}
}
一切都应该是正确的,所有的预制脚本在运动方面应该是完全一样的,但我真的不知道错在哪里。我已经检查了所有内容一百万次,但什么也没找到。
虽然我不知道是否有人会弄清楚,至少从我所展示的内容来看,但任何意见和建议将不胜感激。
确保您正确设置了所有变量 attackTeam
、deffTeam
、attAR_
等(您没有显示分配它们的位置)。我还建议在 CreateController()
.
中使用更清晰的语法
假设当您将 bool attackTeam
设置为 true
时,无需检查 deffTeam
是否为 false
。我会这样写:
if (attackTeam)
{
if (attAR_)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Att_player_AR"), spawnpointA.position, spawnpointA.rotation, 0, new object[] { PV.ViewID });
else
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Att_player_Sniper"), spawnpointC.position, spawnpointC.rotation, 0, new object[] { PV.ViewID });
}
else
{
if (defAR_)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Def_player_AR"), spawnpointD.position, spawnpointD.rotation, 0, new object[] { PV.ViewID });
else
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Def_player_Sniper"), spawnpointB.position, spawnpointB.rotation, 0, new object[] { PV.ViewID });
}
我解决了,问题是我没有破坏别人的摄像头
if (PV.IsMine)
{
EquipItem(0);
}
else
{
Destroy(GetComponentInChildren<Camera>().gameObject);
Destroy(rb);
}
我只在一个团队中移动 2 人或更多人时遇到问题。在一个团队中,它应该运行,每个人都控制自己,但在另一个团队中,如果超过 1 个玩家加入,那么 1 个控制另一个。
我有 4 种玩家类型(2 个用于一个团队,2 个用于另一个团队),所以当我需要它们时,当我开始房间时,我会调用 4 个预制件。
void CreateController()
{
.
.
.
if (attackTeam == true && deffTeam == false && attAR_ == true)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Att_player_AR"), spawnpointA.position, spawnpointA.rotation, 0, new object[] { PV.ViewID });
if (attackTeam == true && deffTeam == false && attSniper_ == true)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Att_player_Sniper"), spawnpointC.position, spawnpointC.rotation, 0, new object[] { PV.ViewID });
if (deffTeam == true && attackTeam == false && defAR_ == true)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Def_player_AR"), spawnpointD.position, spawnpointD.rotation, 0, new object[] { PV.ViewID });
if (deffTeam == true && attackTeam == false && defSniper_ == true)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Def_player_Sniper"), spawnpointB.position, spawnpointB.rotation, 0, new object[] { PV.ViewID });
.
.
.
}
而且到处都应该检查 photonView 是否是我的
PhotonView PV;
.
.
.
void Awake()
{
PV = GetComponent<PhotonView>();
}
void Start()
{
if (PV.IsMine)
{
CreateController();
}
}
在这 4 个预制件中,我为所有这些都配备了 PhotonView,在脚本中,我检查 photonView 是否也是我的
protected virtual void Update()
{
if (!PV.IsMine)
{
return;
}
if (isDead == false && timerOut == false)
{
if (m_characterController && gamePaused == false)
{
// Check if Grounded,Wall etc
CheckIfGrounded();
CheckIfWall();
// Apply Smoothing
SmoothInput();
SmoothSpeed();
SmoothDir();
if (experimental)
SmoothInputMagnitude();
// Calculate Movement
CalculateMovementDirection();
CalculateSpeed();
CalculateFinalMovement();
// Handle Player Movement, Gravity, Jump, Crouch etc.
HandleCrouch();
HandleHeadBob();
HandleRunFOV();
HandleCameraSway();
HandleLanding();
ApplyMovement();
.
.
. }
}
}
一切都应该是正确的,所有的预制脚本在运动方面应该是完全一样的,但我真的不知道错在哪里。我已经检查了所有内容一百万次,但什么也没找到。
虽然我不知道是否有人会弄清楚,至少从我所展示的内容来看,但任何意见和建议将不胜感激。
确保您正确设置了所有变量 attackTeam
、deffTeam
、attAR_
等(您没有显示分配它们的位置)。我还建议在 CreateController()
.
假设当您将 bool attackTeam
设置为 true
时,无需检查 deffTeam
是否为 false
。我会这样写:
if (attackTeam)
{
if (attAR_)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Att_player_AR"), spawnpointA.position, spawnpointA.rotation, 0, new object[] { PV.ViewID });
else
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Att_player_Sniper"), spawnpointC.position, spawnpointC.rotation, 0, new object[] { PV.ViewID });
}
else
{
if (defAR_)
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Def_player_AR"), spawnpointD.position, spawnpointD.rotation, 0, new object[] { PV.ViewID });
else
controller = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "Def_player_Sniper"), spawnpointB.position, spawnpointB.rotation, 0, new object[] { PV.ViewID });
}
我解决了,问题是我没有破坏别人的摄像头
if (PV.IsMine)
{
EquipItem(0);
}
else
{
Destroy(GetComponentInChildren<Camera>().gameObject);
Destroy(rb);
}