You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.5 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class sceneController : MonoBehaviour {
  5. public GameObject player1;
  6. public GameObject player2;
  7. public Camera cameraPlayer1;
  8. public Camera cameraPlayer2;
  9. public GameObject playerPointer;
  10. public float screenAnimationTime;
  11. private thirdPersonController movementP1;
  12. private thirdPersonController movementP2;
  13. private string playerSwapInput = "playerSwap";
  14. // Use this for initialization
  15. void Start () {
  16. movementP1 = player1.GetComponent<thirdPersonController> ();
  17. movementP2 = player2.GetComponent<thirdPersonController> ();
  18. movementP2.enabled = false;
  19. }
  20. // Update is called once per frame
  21. void Update () {
  22. swapCharacters ();
  23. pointAtOther ();
  24. }
  25. private void swapCharacters(){
  26. Camera newCamera;
  27. Camera oldCamera;
  28. if (Input.GetButtonDown(playerSwapInput)) {
  29. Debug.Log("swappy Swap");
  30. movementP1.enabled = !movementP1.enabled;
  31. movementP2.enabled = !movementP2.enabled;
  32. if (movementP1.enabled){
  33. newCamera = cameraPlayer1;
  34. oldCamera = cameraPlayer2;
  35. }else{
  36. newCamera = cameraPlayer2;
  37. oldCamera = cameraPlayer1;
  38. }
  39. newCamera.depth = 0;
  40. oldCamera.depth = 1;
  41. StartCoroutine(swapCameras (newCamera, oldCamera, screenAnimationTime));
  42. //newCamera.rect = new Rect (0.0f,0.0f,1.0f,1.0f);
  43. //oldCamera.rect = new Rect (0.84f, 0.74f, 0.155f, 0.25f);
  44. }
  45. }
  46. private void pointAtOther(){
  47. Transform target;
  48. Camera curCamera;
  49. if (movementP1.enabled) {
  50. target = player2.transform;
  51. curCamera = cameraPlayer1;
  52. } else {
  53. target = player1.transform;
  54. curCamera = cameraPlayer2;
  55. }
  56. Vector3 screenPos = curCamera.WorldToViewportPoint(target.position);
  57. Vector3 guiPos;
  58. //Debug.Log (screenPos);
  59. if (screenPos.x >= 0.0f && screenPos.x <= 1.0f && screenPos.y >= 0.0f && screenPos.y <= 1.0f) {
  60. guiPos = new Vector3 (screenPos.x * Screen.width, screenPos.y * Screen.height, 0.0f);
  61. playerPointer.transform.position = guiPos;
  62. return; // Object center is visible
  63. }
  64. //Debug.Log (screenPos);
  65. if (Mathf.Abs(screenPos.x-0.5f) > Mathf.Abs(screenPos.y-0.5f)) {
  66. screenPos.y = Mathf.Abs((screenPos.y));
  67. if (screenPos.x > 0.0f)
  68. screenPos.x = 1.0f;
  69. else
  70. screenPos.x = 0.0f;
  71. } else {
  72. screenPos.x = Mathf.Abs((screenPos.x));
  73. if (screenPos.y > 0.0f)
  74. screenPos.y = 1.0f;
  75. else
  76. screenPos.y = 0.0f;
  77. }
  78. guiPos = new Vector3 (screenPos.x * Screen.width, screenPos.y * Screen.height, 0.0f);
  79. //guiPos.x = Mathf.Clamp (guiPos.x, 0.0f, Screen.width);
  80. //guiPos.y = Mathf.Clamp (guiPos.y, 0.0f, Screen.height);
  81. playerPointer.transform.position = guiPos;
  82. }
  83. IEnumerator swapCameras (Camera newCamera, Camera oldCamera, float inTime){
  84. Rect tempNewRect = newCamera.rect;
  85. Rect tempOldRect = oldCamera.rect;
  86. for (float i = 0; i < 1; i+=Time.deltaTime/inTime) {
  87. tempNewRect.x = Mathf.Lerp (0.84f, 0.0f, i);
  88. tempNewRect.y = Mathf.Lerp (0.74f, 0.0f, i);
  89. tempNewRect.width = Mathf.Lerp (0.155f, 1.0f, i);
  90. tempNewRect.height = Mathf.Lerp (0.25f, 1.0f, i);
  91. tempOldRect.x = Mathf.Lerp (0.0f, 0.84f, i);
  92. tempOldRect.y = Mathf.Lerp (0.0f, 0.74f, i);
  93. tempOldRect.width = Mathf.Lerp (1.0f, 0.155f, i);
  94. tempOldRect.height = Mathf.Lerp (1.0f, 0.25f, i);
  95. newCamera.rect = tempNewRect;
  96. oldCamera.rect = tempOldRect;
  97. yield return null;
  98. }
  99. Debug.Log ("new rect: " + tempNewRect);
  100. newCamera.rect = new Rect (0.0f,0.0f,1.0f,1.0f);
  101. oldCamera.rect = new Rect (0.84f, 0.74f, 0.155f, 0.25f);
  102. }
  103. }