C# Unity多人游戏:摄像机仅跟随一名玩家
我使用unity的生存射击资源构建了一个游戏。我面临的问题是,摄像头只在主机设备中跟踪主机玩家,而在其他客户端设备中没有跟踪任何玩家 摄像机跟随C# Unity多人游戏:摄像机仅跟随一名玩家,c#,unity3d,C#,Unity3d,我使用unity的生存射击资源构建了一个游戏。我面临的问题是,摄像头只在主机设备中跟踪主机玩家,而在其他客户端设备中没有跟踪任何玩家 摄像机跟随 public class CameraFollow : MonoBehaviour { Transform target; // The position that that camera will be following. public float smoothing = 5f; // The
public class CameraFollow : MonoBehaviour
{
Transform target; // The position that that camera will be following.
public float smoothing = 5f; // The speed with which the camera will be following.
Vector3 offset; // The initial offset from the target.
void Start ()
{
target = GameObject.FindGameObjectWithTag ("Player").transform;
offset = transform.position - target.position;
}
void FixedUpdate()
{
// Create a postion the camera is aiming for based on the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current position and it's target position.
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
LocalPlayerSetup脚本
public class LocalPlayerSetup : NetworkBehaviour
{
void Start()
{
GameObject.FindGameObjectWithTag ("EnemyManager").SetActiveRecursively (true);
if (isLocalPlayer) {
GameObject.FindGameObjectWithTag ("MainCamera").GetComponent<CameraFollow> ().enabled = true;
GetComponent<PlayerMovement> ().enabled = true;
GetComponentInChildren<PlayerShooting> ().enabled = true;
}
}
public类localplayer设置:网络行为
{
void Start()
{
GameObject.FindGameObjectWithTag(“EnemyManager”).SetActiveRecursive(true);
if(IsLocalLayer){
GameObject.FindGameObjectWithTag(“MainCamera”).GetComponent().enabled=true;
GetComponent().enabled=true;
getComponentChildren().enabled=true;
}
}
不要在启动方法中为相机分配目标。而是将其公开,并在本地播放器的启动方法中分配,如下所示:
if (isLocalPlayer) {
("MainCamera").GetComponent<CameraFollow> ().target = transform;
}
if(isLocalPlayer){
(“MainCamera”).GetComponent().target=transform;
}
FindGameObjectWithTag
当有多个对象共享该标记时,可能无法正常工作。我建议您找到其他方法来关联相机和播放器对象。出现错误UnassignedReferenceException:CameraFollow的变量目标尚未分配。您可能需要分配目标变量我已经成功地删除了().enabled=true;这是导致错误的原因。感谢您的帮助