简体   繁体   English

场景多时如何在unity network中引用主摄像头的本地实例。 每个玩家一个

[英]How to reference local instance of main camera in unity networking when there is more than one in the scene. One per player

I'm making a game in multiplayer game in unity that uses the network.我正在使用网络统一制作多人游戏中的游戏。 It uses network manager, network manager HUD and network identity.它使用网络管理器、网络管理器 HUD 和网络标识。 Probably more components as I learn more about unity.随着我对统一性的了解更多,可能会有更多的组件。 The problem is, the main camera spawns in the menu before my player object spawns through network manager.问题是,在我的播放器对象通过网络管理器生成之前,主摄像机会在菜单中生成。 So the lookAt variable isn't properly assigned.所以 lookAt 变量没有正确分配。 I want to assign it either through the camera script or player script after the player has spawned.我想在玩家生成后通过相机脚本或玩家脚本分配它。 The problem is that there are 4 players prefabs and 4 main cameras in the game when its up and running.问题是游戏启动和运行时有 4 个玩家预制件和 4 个主摄像机。 Everything I've tried just reassigns ALL the camera's lookAt variables.我尝试过的一切只是重新分配了所有相机的 lookAt 变量。 How do I assign my just my local camera's lookAt to my local player?我如何将我的本地摄像机的lookAt 分配给我的本地播放器?

  • This is a third person game这是第三人称游戏
  • I really don't want to make the camera a child of the player prefab, this will mess up other parts of my game.我真的不想让相机成为玩家预制件的孩子,这会弄乱我游戏的其他部分。

Don't use a main Camera for every player, there should always only be 1 main camera active in your scene at any given time (hence it being called main camera).不要为每个玩家都使用一个主摄像头,在任何给定时间,您的场景中应该始终只有 1 个主摄像头处于活动状态(因此称为摄像头)。 So if you try to make a call to Camera.Main it will all your main cameras, and not just the player's.因此,如果您尝试调用Camera.Main ,它将调用您所有的主摄像机,而不仅仅是玩家的。

I think the best way to achieve what you want is to make a prefab of a camera (that is not tagged as MainCamera and put your camera script (if you have one) or any other components you want on your camera on that prefab aswell.我认为实现您想要的最佳方法是制作相机的预制件(标记为MainCamera并将您的相机脚本(如果有的话)或您想要在相机上的任何其他组件也放在该预制件上。

Then when your player spawns you instantiate the prefab to make an instance of the camera prefab ment for that player alone, and using the LookAt only on the instanced camera for that player然后,当您的玩家生成时,您将预制件实例化以单独为该玩家制作相机预制件的实例,并且仅在该玩家的实例化相机上使用LookAt

It would look something like the following:它看起来像下面这样:

public GameObject cameraPrefab; //prefab of the camera you want to instance
private Camera playerCam; //this is gonna be a reference to the camera that looks at the player
void SpawnPlayer()
{
    playerCam = Instantiate(cameraPrefab).GetComponent<Camera>(); //instantiate the cameraPrefab and get the camera component of it
    playerCam.transform.LookAt(transform); //get the transform of the instanced camera to look at the transform on which this script is run (In this case assuming your player)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM