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.
 
 
 
 

215 lines
4.7 KiB

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 crossHairPlayer1;
public GameObject crossHairPlayer2;
public GameObject playerPointer;
//public Animator push;
public Animator icon;
public float screenAnimationTime;
private thirdPersonController movementP1;
private thirdPersonController movementP2;
private bool activeP1 = true;
private bool activeP2 = false;
private string playerSwapInput = "playerSwap";
// Call PlayerSound script to allow sounds upon button presses
// NOTE: Make sure the SceneController's inspector has the PlayerAvatar
// attached as an element under its script component.
public PlayerSounds PlayerSoundSource;
// Use this for initialization
void Start () {
//Physics.IgnoreCollision(player1.GetComponent<Collider>(), player2.GetComponent<Collider>(),true);
movementP1 = player1.GetComponent<thirdPersonController> ();
movementP2 = player2.GetComponent<thirdPersonController> ();
movementP2.active = false;
crossHairPlayer2.SetActive (false);
Component[] p1Colliders = player1.GetComponentsInChildren<Collider> ();
Component[] p2Colliders = player2.GetComponentsInChildren<Collider> ();
foreach (Collider p1Col in p1Colliders){
foreach (Collider p2Col in p2Colliders){
Physics.IgnoreCollision(p1Col, p2Col);
}
}
}
// Update is called once per frame
void Update () {
swapCharacters ();
pointAtOther ();
if (Input.GetKeyDown (KeyCode.R))
Application.LoadLevel(Application.loadedLevel);
}
private void swapCharacters(){
Camera newCamera;
Camera oldCamera;
if (Input.GetButtonDown(playerSwapInput)) {
Debug.Log("swappy Swap");
PlayerSoundSource.PlaySwitch(); //Play "Switch Character" sound function
activeP1 = !activeP1;
activeP2 = !activeP2;
if (activeP1){
newCamera = cameraPlayer1;
oldCamera = cameraPlayer2;
crossHairPlayer1.SetActive(true);
crossHairPlayer2.SetActive(false);
icon.SetTrigger("p1");
}else{
newCamera = cameraPlayer2;
oldCamera = cameraPlayer1;
crossHairPlayer1.SetActive(false);
crossHairPlayer2.SetActive(true);
icon.SetTrigger("p2");
}
movementP1.active = activeP1;
movementP2.active = activeP2;
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);
}
}