unity3d - Unity3D多人游戏中相机的奇怪行为
问题描述
我不熟悉统一,并且正在为我的相机的奇怪行为而苦苦挣扎。我正在使用 Photon Plugin 编写在线多人游戏。
奇怪的行为:
玩家 1 登录
--> 一切都好
玩家2登录
--> Player1 的相机切换到 Player2 角色
--> Player2的Camera切换到Player1角色</p>
玩家 3 登录
--> Player 1 和 Player2 的相机切换到 Player3 角色
--> Player3 的相机切换到 Player2 角色。</p>
(但运动控制对每个玩家都有效)
我得到了一个角色的预制件,它在检查器中安装了一个摄像头。
这是我的初始化代码:
public class NetworkPlayer : Photon.Pun.MonoBehaviourPun, Photon.Pun.IPunObservable
{
public Animator anim;
private Vector3 correctPlayerPos;
private Quaternion correctPlayerRot;
public GameObject myCam;
// Start is called before the first frame update
void Start()
{
if(photonView.IsMine)
{
Debug.Log("PhotonView.IsMine == true");
//Kamera und Steuerung aktivieren
myCam = GameObject.Find("Camera");
myCam.SetActive(true);
GetComponent<PlayerMovement>().enabled = true;
Debug.Log("Steuerung und Cam aktiviert...");
}
}
// Update is called once per frame
void Update()
{
if(!photonView.IsMine)
{
transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime*5);
transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
}
}
//Exchange Position data
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if(stream.IsWriting)
{
//Send data to others
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
stream.SendNext(anim.GetBool("Run"));
}
else
{
//Receive data from others
this.correctPlayerPos = (Vector3) stream.ReceiveNext();
this.correctPlayerRot = (Quaternion) stream.ReceiveNext();
anim.SetBool("Run", (bool) stream.ReceiveNext());
}
}
}
我还尝试通过检查器连接相机,而不是像上面的代码示例中那样搜索它。希望任何人都可以帮助我:(
感谢您的时间!
解决方案
我的猜测是你SetAtive(true)
在播放器上做了,但从你的照片来看,相机默认情况下似乎是激活的!因此,每个新相机都添加到 Hierachy 中先前相机的下方,这使其成为顶部渲染的相机。
您可以简单地禁用Camera
播放器预制件中的 。
无论如何,您还应该主动为脚本中的其他玩家禁用相机,这样您就不会忘记/不必依赖将您的预制件设置为特定状态
public class NetworkPlayer : Photon.Pun.MonoBehaviourPun, Photon.Pun.IPunObservable
{
public Animator anim;
// you are referncing this for the prefab via the inspector
// I would also use the proper type as a safety
// this way you can't by accident reference something else here but a Camera
public Camera myCam;
// you should also this already via the inspector
public PlayerMovement playerMovement;
private Vector3 correctPlayerPos;
private Quaternion correctPlayerRot;
// Use Awake to gather components and references as fallback
// if they where not referenced via the Inspector
private void Awake()
{
FetchComponents();
}
// Fetches components only if they where not referenced via the Inspector
// this saves unnecessary GetComponent calls which are quite expensive
//
// + personal pro tip: By using the context menu you can now also in the Inspector simply click on FetchComponents
// This way you
// a) don't have to search and drag them in manually
// b) can already see if it would also work later on runtime
[ContextMenu(nameof(FetchComponents))]
private void FetchComponents()
{
if(!playerMovement) playerMovement = GetComponent<PlayerMovement>();
if(!anim) anim = GetComponent<Animator>();
// Use GetComponnetInChildren to retrieve the component on yourself
// or recursive any of your children!
// Pass true in order to also get disabled or inactive ones
if(!myCam) myCam = GetComponentInChildren<Camera>(true);
}
// Now do your thing not only for the player but also actively disable the stuff
// on all other players
private void Start()
{
var isMine = photonView.IsMine;
Debug.Log($"PhotonView.IsMine == {isMine}");
// enable the cam for you, disable them for others!
myCam.gameObject.SetActive(isMine);
// enable controls for you, disable them for others
playerMovement.enabled = isMine;
Debug.Log($"Steuerung und Cam {isMine ? "" : "de"}aktiviert...");
}
...
}
推荐阅读
- apache-camel - Camel netty4-http 在请求中设置了无效的主机 http 标头
- flutter - 有没有办法在一个容器中包含多个孩子?
- silverstripe - Silverstripe 4 silvershop-core paypal-expr. 税费和运费
- android - 使用此命令在移动设备中运行离子应用程序时出错(离子科尔多瓦运行安卓)
- android - Android 导航组件 - 在包含的模块图之间导航
- android - 在 android ExoPlayer 中添加用于播放加密 .m3u8 流的参数
- netbeans - Netbeans.conf:用户主页的变量是什么?
- javascript - 用户选择:没有在 Safari 中表现不同
- r - 如何使用带有复选框事件 Shiny 的 observeEvent
- javascript - 当我在 html 和 css 中向下滚动时,如何使图像位于菜单栏后面?