Fancy camera for couch coop example, and player list. (#3821)

This commit is contained in:
JesusLuvsYooh 2024-05-14 12:10:28 +01:00 committed by GitHub
parent 534a57f245
commit c62d7e5c07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 342 additions and 74 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,73 @@
using UnityEngine;
namespace Mirror.Examples.CouchCoop
{
public class CameraViewForAll : MonoBehaviour
{
public Transform cameraTransform;
public float camSpeed = 2.0f;
public float orthoSizeSpeed = 2.0f;
public Camera mainCamera;
public float cameraZ = -5;
public float cameraBufferX = 0.1f;
public float cameraBufferY = 0.1f;
public float minOrthographicSize = 0.1f;
public float targetYPosition = 4.5f; // Optional Y position if cameras rotated
private Vector2Int boundsMin;
private Vector2Int boundsMax;
private Vector3 targetCameraPosition;
private float targetOrthographicSize;
private void Update()
{
if (CouchPlayer.playersList.Count > 0)
{
CalculateBounds();
CalculateTargetCameraPosAndSize();
MoveCamera();
}
}
private void CalculateBounds()
{
boundsMin = new Vector2Int(int.MaxValue, int.MaxValue);
boundsMax = new Vector2Int(int.MinValue, int.MinValue);
foreach (GameObject player in CouchPlayer.playersList)
{
Vector3 playerPosition = player.transform.position;
boundsMin.x = Mathf.Min(boundsMin.x, Mathf.FloorToInt(playerPosition.x));
boundsMin.y = Mathf.Min(boundsMin.y, Mathf.FloorToInt(playerPosition.y));
boundsMax.x = Mathf.Max(boundsMax.x, Mathf.CeilToInt(playerPosition.x));
boundsMax.y = Mathf.Max(boundsMax.y, Mathf.CeilToInt(playerPosition.y));
}
boundsMin.x -= Mathf.FloorToInt(cameraBufferX);
boundsMin.y -= Mathf.FloorToInt(cameraBufferY);
boundsMax.x += Mathf.CeilToInt(cameraBufferX);
boundsMax.y += Mathf.CeilToInt(cameraBufferY);
}
private void CalculateTargetCameraPosAndSize()
{
float aspectRatio = (float)Screen.width / Screen.height;
float requiredOrthographicSizeX = Mathf.Max((boundsMax.x - boundsMin.x) / 2 / aspectRatio, minOrthographicSize / aspectRatio);
float requiredOrthographicSizeY = Mathf.Max(boundsMax.y - boundsMin.y / 2, minOrthographicSize);
targetOrthographicSize = Mathf.Max(requiredOrthographicSizeX, requiredOrthographicSizeY);
float cameraX = (boundsMax.x + boundsMin.x) / 2;
float cameraY = targetYPosition != 0.0f ? targetYPosition : (boundsMax.y + boundsMin.y) / 2;
targetCameraPosition = new Vector3(cameraX, cameraY, cameraZ);
}
private void MoveCamera()
{
cameraTransform.position = Vector3.Lerp(cameraTransform.position, targetCameraPosition, camSpeed * Time.deltaTime);
mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, targetOrthographicSize, orthoSizeSpeed * Time.deltaTime);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 73edff93783204b298f805477ae30ecd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
@ -20,6 +21,23 @@ public class CouchPlayer : NetworkBehaviour
public int playerNumber = 0;
public Text textPlayerNumber;
// a list of players, is used for camera
public readonly static List<GameObject> playersList = new List<GameObject>();
public void Start()
{
playersList.Add(this.gameObject);
// print("playersList: " + playersList.Count);
SetPlayerUI();
}
public void OnDestroy()
{
playersList.Remove(this.gameObject);
// print("playersList: " + playersList.Count);
}
public override void OnStartAuthority()
{
this.enabled = true;
@ -39,11 +57,6 @@ public override void OnStartAuthority()
}
}
public void Start()
{
SetPlayerUI();
}
void Update()
{
if (!Application.isFocused) return;