public void KeepPlayerInCameraView()
{
Vector2 playerPosScreen = Camera.main.WorldToScreenPoint(transform.position);
if (playerPosScreen.x + screenEdgeHorizontal > Mathf.Abs(Screen.width) && (playerPosScreen.y - screenEdgeVertical < 0))
{
rb.position = Camera.main.ScreenToWorldPoint(new Vector3(Mathf.Abs(Screen.width) - screenEdgeHorizontal, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
}
else if (playerPosScreen.x + screenEdgeHorizontal > Mathf.Abs(Screen.width))
{
rb.position = Camera.main.ScreenToWorldPoint(new Vector3(Mathf.Abs(Screen.width) - screenEdgeHorizontal, playerPosScreen.y, transform.position.z - Camera.main.transform.position.z));
}
else if (playerPosScreen.x - screenEdgeHorizontal < 0f && (playerPosScreen.y - screenEdgeVertical < 0))
{
rb.position = Camera.main.ScreenToWorldPoint(new Vector3(screenEdgeHorizontal, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
}
else if (playerPosScreen.x - screenEdgeHorizontal < 0f)
{
rb.position = Camera.main.ScreenToWorldPoint(new Vector3(screenEdgeHorizontal, playerPosScreen.y, transform.position.z - Camera.main.transform.position.z));
}
else if ((playerPosScreen.y - screenEdgeVertical < 0) && (playerPosScreen.x + screenEdgeHorizontal > Mathf.Abs(Screen.width)))
{
rb.position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width - screenEdgeHorizontal, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
}
else if ((playerPosScreen.y - screenEdgeVertical < 0) && (playerPosScreen.x - screenEdgeHorizontal < 0f))
{
rb.position = Camera.main.ScreenToWorldPoint(new Vector3(screenEdgeHorizontal, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
}
else if (playerPosScreen.y - screenEdgeVertical < 0)
{
rb.position = Camera.main.ScreenToWorldPoint(new Vector3(playerPosScreen.x, screenEdgeVertical, transform.position.z - Camera.main.transform.position.z));
}
}