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.

201 lines
4.3 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 crossHairPlayer1;
  10. public GameObject crossHairPlayer2;
  11. public GameObject playerPointer;
  12. public float screenAnimationTime;
  13. private thirdPersonController movementP1;
  14. private thirdPersonController movementP2;
  15. private bool activeP1 = true;
  16. private bool activeP2 = false;
  17. private string playerSwapInput = "playerSwap";
  18. // Use this for initialization
  19. void Start () {
  20. //Physics.IgnoreCollision(player1.GetComponent<Collider>(), player2.GetComponent<Collider>(),true);
  21. movementP1 = player1.GetComponent<thirdPersonController> ();
  22. movementP2 = player2.GetComponent<thirdPersonController> ();
  23. movementP2.active = false;
  24. crossHairPlayer2.SetActive (false);
  25. Component[] p1Colliders = player1.GetComponentsInChildren<Collider> ();
  26. Component[] p2Colliders = player2.GetComponentsInChildren<Collider> ();
  27. foreach (Collider p1Col in p1Colliders){
  28. foreach (Collider p2Col in p2Colliders){
  29. Physics.IgnoreCollision(p1Col, p2Col);
  30. }
  31. }
  32. }
  33. // Update is called once per frame
  34. void Update () {
  35. swapCharacters ();
  36. pointAtOther ();
  37. if (Input.GetKeyDown(KeyCode.R))
  38. Application.LoadLevel(Application.loadedLevel);
  39. }
  40. private void swapCharacters(){
  41. Camera newCamera;
  42. Camera oldCamera;
  43. if (Input.GetButtonDown(playerSwapInput)) {
  44. Debug.Log("swappy Swap");
  45. activeP1 = !activeP1;
  46. activeP2 = !activeP2;
  47. if (activeP1){
  48. newCamera = cameraPlayer1;
  49. oldCamera = cameraPlayer2;
  50. crossHairPlayer1.SetActive(true);
  51. crossHairPlayer2.SetActive(false);
  52. }else{
  53. newCamera = cameraPlayer2;
  54. oldCamera = cameraPlayer1;
  55. crossHairPlayer1.SetActive(false);
  56. crossHairPlayer2.SetActive(true);
  57. }
  58. movementP1.active = activeP1;
  59. movementP2.active = activeP2;
  60. newCamera.depth = 0;
  61. oldCamera.depth = 1;
  62. StartCoroutine(swapCameras (newCamera, oldCamera, screenAnimationTime));
  63. //newCamera.rect = new Rect (0.0f,0.0f,1.0f,1.0f);
  64. //oldCamera.rect = new Rect (0.84f, 0.74f, 0.155f, 0.25f);
  65. }
  66. }
  67. private void pointAtOther(){
  68. Transform target;
  69. Camera curCamera;
  70. if (movementP1.enabled) {
  71. target = player2.transform;
  72. curCamera = cameraPlayer1;
  73. } else {
  74. target = player1.transform;
  75. curCamera = cameraPlayer2;
  76. }
  77. Vector3 screenPos = curCamera.WorldToViewportPoint(target.position);
  78. Vector3 guiPos;
  79. //Debug.Log (screenPos);
  80. if (screenPos.x >= 0.0f && screenPos.x <= 1.0f && screenPos.y >= 0.0f && screenPos.y <= 1.0f) {
  81. guiPos = new Vector3 (screenPos.x * Screen.width, screenPos.y * Screen.height, 0.0f);
  82. playerPointer.transform.position = guiPos;
  83. return; // Object center is visible
  84. }
  85. //Debug.Log (screenPos);
  86. if (Mathf.Abs(screenPos.x-0.5f) > Mathf.Abs(screenPos.y-0.5f)) {
  87. screenPos.y = Mathf.Abs((screenPos.y));
  88. if (screenPos.x > 0.0f)
  89. screenPos.x = 1.0f;
  90. else
  91. screenPos.x = 0.0f;
  92. } else {
  93. screenPos.x = Mathf.Abs((screenPos.x));
  94. if (screenPos.y > 0.0f)
  95. screenPos.y = 1.0f;
  96. else
  97. screenPos.y = 0.0f;
  98. }
  99. guiPos = new Vector3 (screenPos.x * Screen.width, screenPos.y * Screen.height, 0.0f);
  100. //guiPos.x = Mathf.Clamp (guiPos.x, 0.0f, Screen.width);
  101. //guiPos.y = Mathf.Clamp (guiPos.y, 0.0f, Screen.height);
  102. playerPointer.transform.position = guiPos;
  103. }
  104. IEnumerator swapCameras (Camera newCamera, Camera oldCamera, float inTime){
  105. Rect tempNewRect = newCamera.rect;
  106. Rect tempOldRect = oldCamera.rect;
  107. for (float i = 0; i < 1; i+=Time.deltaTime/inTime) {
  108. tempNewRect.x = Mathf.Lerp (0.84f, 0.0f, i);
  109. tempNewRect.y = Mathf.Lerp (0.74f, 0.0f, i);
  110. tempNewRect.width = Mathf.Lerp (0.155f, 1.0f, i);
  111. tempNewRect.height = Mathf.Lerp (0.25f, 1.0f, i);
  112. tempOldRect.x = Mathf.Lerp (0.0f, 0.84f, i);
  113. tempOldRect.y = Mathf.Lerp (0.0f, 0.74f, i);
  114. tempOldRect.width = Mathf.Lerp (1.0f, 0.155f, i);
  115. tempOldRect.height = Mathf.Lerp (1.0f, 0.25f, i);
  116. newCamera.rect = tempNewRect;
  117. oldCamera.rect = tempOldRect;
  118. yield return null;
  119. }
  120. Debug.Log ("new rect: " + tempNewRect);
  121. newCamera.rect = new Rect (0.0f,0.0f,1.0f,1.0f);
  122. oldCamera.rect = new Rect (0.84f, 0.74f, 0.155f, 0.25f);
  123. }
  124. }