Here’s the code, I doubled checked seems to be fine. It works for the player joining the game but not for the Host, also if the joining player leaves the game, the camera for the host works again.
public class CameraController : NetworkBehaviour
{
[SerializeField] private Transform playerCameraTransform = null;
[SerializeField] private float speed = 20f;
[SerializeField] private float screenBorderThickness = 10f;
[SerializeField] private Vector2 screenXLimits = Vector2.zero;
[SerializeField] private Vector2 screenZLimits = Vector2.zero;
private Vector2 previousInput;
private Controls controls;
public override void OnStartAuthority()
{
playerCameraTransform.gameObject.SetActive(true);
controls = new Controls();
controls.Player.MoveCamera.performed += SetPreviousInput;
controls.Player.MoveCamera.canceled += SetPreviousInput;
controls.Enable();
}
[ClientCallback]
private void Update()
{
if (!hasAuthority|| !Application.isFocused) {return;}
UpdateCameraPosition();
}
private void UpdateCameraPosition()
{
Vector3 pos = playerCameraTransform.position;
if (previousInput == Vector2.zero)
{
Vector3 cursorMovment = Vector3.zero;
Vector2 cursorPosition = Mouse.current.position.ReadValue();
if (cursorPosition.y >= Screen.height - screenBorderThickness)
{
cursorMovment.z +=1;
}
else if (cursorPosition.y <= screenBorderThickness)
{
cursorMovment.z -=1;
}
if (cursorPosition.x >= Screen.width - screenBorderThickness)
{
cursorMovment.x +=1;
}
else if (cursorPosition.x <= screenBorderThickness)
{
cursorMovment.x -=1;
}
pos += cursorMovment.normalized * speed * Time.deltaTime;
}
else
{
pos += new Vector3(previousInput.x, 0f, previousInput.y) * speed * Time.deltaTime;
}
pos.x = Mathf.Clamp(pos.x, screenXLimits.x, screenXLimits.y);
pos.z = Mathf.Clamp(pos.z, screenZLimits.x, screenZLimits.y);
playerCameraTransform.position = pos;
}
private void SetPreviousInput(InputAction.CallbackContext ctx)
{
previousInput = ctx.ReadValue<Vector2>();
}
}