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.

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