Advertisement
Guest User

CameraInAction

a guest
Apr 19th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class CameraInAction : MonoBehaviour {
  6.  
  7.     public GameObject[] controlsUI;
  8.     public GameObject startButton;
  9.  
  10.  
  11.     private GameObject[] players;
  12.     private Vector3 CamOldPos;
  13.     private GameObject oldHitGO;
  14.     private bool start_action = false;
  15.  
  16.  
  17.     void Start () {
  18.         CamOldPos = transform.position;
  19.         players = GameObject.FindGameObjectsWithTag("Player");
  20.     }
  21.  
  22.     void Update () {
  23.         if (!start_action)
  24.             MovingSquad ();
  25.     }
  26.  
  27.     void CameraTarget () {
  28.  
  29.         gameObject.GetComponent<SmoothFollow>().target = players[Random.Range(0, players.Length)];
  30.         oldHitGO = players[Random.Range(0, players.Length)];
  31.         oldHitGO.GetComponent<GunController> ().underControl = true;
  32.     }
  33.  
  34.     void CameraTop () {
  35.         transform.position = CamOldPos;
  36.     }
  37.  
  38.     public void StartAction () {
  39.         start_action = true;
  40.         foreach(var c in controlsUI){ c.SetActive(true);}
  41.         startButton.SetActive(false);
  42.  
  43.         if(oldHitGO != null)
  44.         {
  45.             gameObject.GetComponent<SmoothFollow>().target = oldHitGO;
  46.             oldHitGO.GetComponent<GunController> ().underControl = true;
  47.         } else {
  48.  
  49.         CameraTarget();
  50.         }
  51.     }
  52.     void MovingSquad () {
  53.  
  54.         Ray ray = gameObject.camera.ScreenPointToRay(Input.mousePosition);
  55.         RaycastHit hit;
  56.  
  57.         if (Input.GetMouseButtonUp(0)) {
  58.             if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
  59.                 if(hit.collider.tag != "Player" && oldHitGO != null){
  60.                         oldHitGO.transform.position = new Vector3(hit.point.x, oldHitGO.transform.position.y, hit.point.z); //присваиваем координаты
  61.                     oldHitGO.GetComponent<GunController> ().underControl = false; //отключаем управление
  62.                     oldHitGO = null; //удаляем управляемый объект
  63.                 }
  64.  
  65.                 if (hit.collider.tag == "Player") { //Проверяем, если игрок нажал на одну из пушек, то берём её под управление
  66.    
  67.                     if (oldHitGO != null)
  68.                         oldHitGO.GetComponent<GunController> ().underControl = false; //Выключаем управление у прошлой цели
  69.  
  70.                     oldHitGO = hit.collider.gameObject; //Присваиваем новую цель
  71.                     oldHitGO.GetComponent<GunController> ().underControl = true; // включаем управление у новой цели
  72.                 }
  73.            
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement