// Update is called once per frame void Update() { // Error checking. This script only works on 2 players. if (PlayerList.Count != PLAYER_COUNT) { throw new UnityException("Too many players assigned to MultiPlayerCamera!"); } // Vector3 to store the center point Vector3 centerPoint; // Get a list of alive players var alivePlayers = PlayerList.Where(x => x.GetComponent <RPGPlayer>().IsAlive).ToList(); // React accordingly to that if (alivePlayers.Count > 1) { // Get the half distance between the players Vector2 deltaPos = PlayerList[1].transform.position - PlayerList[0].transform.position; deltaPos *= 0.5f; // PLAYER_COUNT // Calculate the centerpoint centerPoint = (Vector2)PlayerList[0].transform.position + deltaPos; } else if (alivePlayers.Count > 0) { centerPoint = alivePlayers.First().transform.position; } else { // Game over, they're all dead return; } if (DeadZoneEnabled && !moveTowardsCenterPoint) { // Check if this point is outside the deadzone, if so, then we move Vector2 topLeftBound = new Vector2(LeftBound + LeftDeadZonePadding, TopBound - TopDeadZonePadding); Vector2 botRightBound = new Vector2(RightBound - RightDeadZonePadding, BottomBound + BottomDeadZonePadding); // Only move if we are out of the bounds if (! ( topLeftBound.x < centerPoint.x && botRightBound.x > centerPoint.x && topLeftBound.y > centerPoint.y && botRightBound.y < centerPoint.y ) ) { moveTowardsCenterPoint = true; } } if (!DeadZoneEnabled || moveTowardsCenterPoint) { // Get direction to the center point Vector2 posDelta = centerPoint - transform.position; // Get the distance we travel this frame Vector3 moveDelta = posDelta.normalized * CameraSnapSpeed * (float)TimeManager.GetDeltaTime(TimeManager.TimeType.Game); // Clamp the values if (Mathf.Abs(moveDelta.x) > Mathf.Abs(posDelta.x)) { moveDelta.x = posDelta.x; } if (Mathf.Abs(moveDelta.y) > Mathf.Abs(posDelta.y)) { moveDelta.y = posDelta.y; } // Get the distance we travel this frame and hence our new position Vector3 newPos = transform.position + moveDelta; transform.position = newPos; if ( moveTowardsCenterPoint && (transform.position - centerPoint).sqrMagnitude < DEADZONE_STOP_MOVEMENT_ACCURACY * DEADZONE_STOP_MOVEMENT_ACCURACY ) { moveTowardsCenterPoint = false; } } // Send tile map the info for activating/deactivating tiles that are (not)in view TileMapReference.ActivateTiles(new Vector2(LeftBound, TopBound), new Vector2(RightBound, BottomBound)); }