我正在 Unity 中制作一个 2D 游戏,它是在线的(使用网络代码)。当播放器生成时,相机也会随之生成。我能够让每个摄像机正确地跟随每个玩家(脚本在下面)但是由于某种原因,每当另一个玩家出现时,每个玩家屏幕都会切换到新玩家的摄像机(包括新玩家)。
这是让相机跟随玩家的脚本:
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform target; // The target object to follow (the player)
public float smoothSpeed = 0f; // The smoothing speed
public Vector3 offset; // The offset from the target's position
private void FixedUpdate()
{
// Only move the camera if there is a valid target
if (target != null)
{
// Calculate the desired position for the camera based on the target's position and the offset
Vector3 desiredPosition = target.position + offset;
// Smoothly move the camera towards the desired position
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
// Set the camera's position to the smoothed position
transform.position = smoothedPosition;
}
}
}
我尝试使用 Chat Gpt-3,但无济于事,它没有改变任何东西。它能够在游戏的其他部分帮助我,但不是这个相机问题
编辑:我在网上发现了一个可能有帮助的问题:https://answers.unity.com/questions/1128271/network-multiple-cameras.html
我将此添加到我的代码中:
void Update()
{
if (!IsLocalPlayer)
{
cam.enabled = false;
return;
}
}
但现在相机并没有像它应该的那样真正跟随玩家