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.

197 lines
4.2 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. }
  38. private void swapCharacters(){
  39. Camera newCamera;
  40. Camera oldCamera;
  41. if (Input.GetButtonDown(playerSwapInput)) {
  42. Debug.Log("swappy Swap");
  43. activeP1 = !activeP1;
  44. activeP2 = !activeP2;
  45. if (activeP1){
  46. newCamera = cameraPlayer1;
  47. oldCamera = cameraPlayer2;
  48. crossHairPlayer1.SetActive(true);
  49. crossHairPlayer2.SetActive(false);
  50. }else{
  51. newCamera = cameraPlayer2;
  52. oldCamera = cameraPlayer1;
  53. crossHairPlayer1.SetActive(false);
  54. crossHairPlayer2.SetActive(true);
  55. }
  56. movementP1.active = activeP1;
  57. movementP2.active = activeP2;
  58. newCamera.depth = 0;
  59. oldCamera.depth = 1;
  60. StartCoroutine(swapCameras (newCamera, oldCamera, screenAnimationTime));
  61. //newCamera.rect = new Rect (0.0f,0.0f,1.0f,1.0f);
  62. //oldCamera.rect = new Rect (0.84f, 0.74f, 0.155f, 0.25f);
  63. }
  64. }
  65. private void pointAtOther(){
  66. Transform target;
  67. Camera curCamera;
  68. if (movementP1.enabled) {
  69. target = player2.transform;
  70. curCamera = cameraPlayer1;
  71. } else {
  72. target = player1.transform;
  73. curCamera = cameraPlayer2;
  74. }
  75. Vector3 screenPos = curCamera.WorldToViewportPoint(target.position);
  76. Vector3 guiPos;
  77. //Debug.Log (screenPos);
  78. if (screenPos.x >= 0.0f && screenPos.x <= 1.0f && screenPos.y >= 0.0f && screenPos.y <= 1.0f) {
  79. guiPos = new Vector3 (screenPos.x * Screen.width, screenPos.y * Screen.height, 0.0f);
  80. playerPointer.transform.position = guiPos;
  81. return; // Object center is visible
  82. }
  83. //Debug.Log (screenPos);
  84. if (Mathf.Abs(screenPos.x-0.5f) > Mathf.Abs(screenPos.y-0.5f)) {
  85. screenPos.y = Mathf.Abs((screenPos.y));
  86. if (screenPos.x > 0.0f)
  87. screenPos.x = 1.0f;
  88. else
  89. screenPos.x = 0.0f;
  90. } else {
  91. screenPos.x = Mathf.Abs((screenPos.x));
  92. if (screenPos.y > 0.0f)
  93. screenPos.y = 1.0f;
  94. else
  95. screenPos.y = 0.0f;
  96. }
  97. guiPos = new Vector3 (screenPos.x * Screen.width, screenPos.y * Screen.height, 0.0f);
  98. //guiPos.x = Mathf.Clamp (guiPos.x, 0.0f, Screen.width);
  99. //guiPos.y = Mathf.Clamp (guiPos.y, 0.0f, Screen.height);
  100. playerPointer.transform.position = guiPos;
  101. }
  102. IEnumerator swapCameras (Camera newCamera, Camera oldCamera, float inTime){
  103. Rect tempNewRect = newCamera.rect;
  104. Rect tempOldRect = oldCamera.rect;
  105. for (float i = 0; i < 1; i+=Time.deltaTime/inTime) {
  106. tempNewRect.x = Mathf.Lerp (0.84f, 0.0f, i);
  107. tempNewRect.y = Mathf.Lerp (0.74f, 0.0f, i);
  108. tempNewRect.width = Mathf.Lerp (0.155f, 1.0f, i);
  109. tempNewRect.height = Mathf.Lerp (0.25f, 1.0f, i);
  110. tempOldRect.x = Mathf.Lerp (0.0f, 0.84f, i);
  111. tempOldRect.y = Mathf.Lerp (0.0f, 0.74f, i);
  112. tempOldRect.width = Mathf.Lerp (1.0f, 0.155f, i);
  113. tempOldRect.height = Mathf.Lerp (1.0f, 0.25f, i);
  114. newCamera.rect = tempNewRect;
  115. oldCamera.rect = tempOldRect;
  116. yield return null;
  117. }
  118. Debug.Log ("new rect: " + tempNewRect);
  119. newCamera.rect = new Rect (0.0f,0.0f,1.0f,1.0f);
  120. oldCamera.rect = new Rect (0.84f, 0.74f, 0.155f, 0.25f);
  121. }
  122. }