using UnityEngine; using System.Collections; using UnityEngine.UI; public class sceneController : MonoBehaviour { public GameObject player1; public GameObject player2; public Camera cameraPlayer1; public Camera cameraPlayer2; public GameObject playerPointer; public float screenAnimationTime; private thirdPersonController movementP1; private thirdPersonController movementP2; private string playerSwapInput = "playerSwap"; // Use this for initialization void Start () { movementP1 = player1.GetComponent (); movementP2 = player2.GetComponent (); movementP2.enabled = false; } // Update is called once per frame void Update () { swapCharacters (); pointAtOther (); } private void swapCharacters(){ Camera newCamera; Camera oldCamera; if (Input.GetButtonDown(playerSwapInput)) { Debug.Log("swappy Swap"); movementP1.enabled = !movementP1.enabled; movementP2.enabled = !movementP2.enabled; if (movementP1.enabled){ newCamera = cameraPlayer1; oldCamera = cameraPlayer2; }else{ newCamera = cameraPlayer2; oldCamera = cameraPlayer1; } newCamera.depth = 0; oldCamera.depth = 1; StartCoroutine(swapCameras (newCamera, oldCamera, screenAnimationTime)); //newCamera.rect = new Rect (0.0f,0.0f,1.0f,1.0f); //oldCamera.rect = new Rect (0.84f, 0.74f, 0.155f, 0.25f); } } private void pointAtOther(){ Transform target; Camera curCamera; if (movementP1.enabled) { target = player2.transform; curCamera = cameraPlayer1; } else { target = player1.transform; curCamera = cameraPlayer2; } Vector3 screenPos = curCamera.WorldToViewportPoint(target.position); Vector3 guiPos; //Debug.Log (screenPos); if (screenPos.x >= 0.0f && screenPos.x <= 1.0f && screenPos.y >= 0.0f && screenPos.y <= 1.0f) { guiPos = new Vector3 (screenPos.x * Screen.width, screenPos.y * Screen.height, 0.0f); playerPointer.transform.position = guiPos; return; // Object center is visible } //Debug.Log (screenPos); if (Mathf.Abs(screenPos.x-0.5f) > Mathf.Abs(screenPos.y-0.5f)) { screenPos.y = Mathf.Abs((screenPos.y)); if (screenPos.x > 0.0f) screenPos.x = 1.0f; else screenPos.x = 0.0f; } else { screenPos.x = Mathf.Abs((screenPos.x)); if (screenPos.y > 0.0f) screenPos.y = 1.0f; else screenPos.y = 0.0f; } guiPos = new Vector3 (screenPos.x * Screen.width, screenPos.y * Screen.height, 0.0f); //guiPos.x = Mathf.Clamp (guiPos.x, 0.0f, Screen.width); //guiPos.y = Mathf.Clamp (guiPos.y, 0.0f, Screen.height); playerPointer.transform.position = guiPos; } IEnumerator swapCameras (Camera newCamera, Camera oldCamera, float inTime){ Rect tempNewRect = newCamera.rect; Rect tempOldRect = oldCamera.rect; for (float i = 0; i < 1; i+=Time.deltaTime/inTime) { tempNewRect.x = Mathf.Lerp (0.84f, 0.0f, i); tempNewRect.y = Mathf.Lerp (0.74f, 0.0f, i); tempNewRect.width = Mathf.Lerp (0.155f, 1.0f, i); tempNewRect.height = Mathf.Lerp (0.25f, 1.0f, i); tempOldRect.x = Mathf.Lerp (0.0f, 0.84f, i); tempOldRect.y = Mathf.Lerp (0.0f, 0.74f, i); tempOldRect.width = Mathf.Lerp (1.0f, 0.155f, i); tempOldRect.height = Mathf.Lerp (1.0f, 0.25f, i); newCamera.rect = tempNewRect; oldCamera.rect = tempOldRect; yield return null; } Debug.Log ("new rect: " + tempNewRect); newCamera.rect = new Rect (0.0f,0.0f,1.0f,1.0f); oldCamera.rect = new Rect (0.84f, 0.74f, 0.155f, 0.25f); } }