CameraController.cs
using Mirror;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class CameraController : NetworkBehaviour
{
[SerializeField]
private Transform playerCameraTransform = null;
[SerializeField]
private float speed = 20f;
[SerializeField]
private float screenBorderThickness = 10f;
[SerializeField]
private Vector2 screenXLimit = Vector2.zero;
[SerializeField]
private Vector2 screenZLimit = 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 cursorMovement = Vector3.zero;
Vector2 cursorPosition = Mouse.current.position.ReadValue();
if (cursorPosition.y >= Screen.height - screenBorderThickness)
{
cursorMovement.z += 1;
}
else if (cursorPosition.y <= screenBorderThickness)
{
cursorMovement.z -= 1;
}
if (cursorPosition.x >= Screen.width - screenBorderThickness)
{
cursorMovement.x += 1;
}
else if (cursorPosition.x <= screenBorderThickness)
{
cursorMovement.x -= 1;
}
pos += cursorMovement.normalized * speed * Time.deltaTime;
}
else
{
pos += new Vector3(previousInput.x, 0f, previousInput.y) * speed * Time.deltaTime;
}
pos.x = Mathf.Clamp(pos.x, screenXLimit.x, screenXLimit.y);
pos.z = Mathf.Clamp(pos.z, screenZLimit.x, screenZLimit.y);
playerCameraTransform.position = pos;
}
private void SetPreviousInput(InputAction.CallbackContext ctx)
{
previousInput = ctx.ReadValue<Vector2>();
}
}
I’ll send you the video for the second problem in a bit.
Edit:
2nd Problem
Yesterday when I was testing around, both my host and client screens were flickering but today, for some reason, even though I didn’t touch anything, when the client joins the host screen stops flickering and moves away vertically on it’s own as you can see in the video when I am unable to move the screen vertically myself with the camera input controls.
I am also unable to click on the minimap to change position of the camera.
P.S: Sorry for the music in the video.