Unity多人游戏:仅限摄像机跟随一名玩家
问题描述:
我使用生存射击游戏资产创建了一个游戏。我面临的问题是,摄像机只跟随主机设备中的主机玩家,而其他客户端没有任何玩家跟随设备。Unity多人游戏:仅限摄像机跟随一名玩家
相机按照
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;
}
}
答
不分配目标的相机在其Start方法。相反,使公众和本土球员的这样的启动方法为它分配:
if (isLocalPlayer) {
("MainCamera").GetComponent<CameraFollow>().target = transform;
}
+0
给出错误** UnassignedReferenceException:CameraFollow的变量目标尚未分配。 您可能需要在检查器中指定CameraFollow脚本的目标变量。** –
+0
我已经重点删除了
'FindGameObjectWithTag'可能不表现良好,当有多个对象共享该标签。我建议你找一些其他方式来关联相机和播放器对象。 – rutter