Advertisement
NuclearLazarus

Unity3D RTS Camera Script

Jun 19th, 2011
2,859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.57 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class CameraMovement : MonoBehaviour {
  5.     //-------------------------------------------------------//
  6.     private Vector2 mouseButton2DownPoint, mouseButton2UpPoint;   // точки, в которых мы нажали и отпустили правую кнопку мыши (по умолчанию отвечает за движение)
  7.     private Vector2 turnButtonDownPoint;                          // точка, в которой мы зажали клавишу поворота
  8.     private bool    dragMouse, turnCamera;                         // перемещать и поворачивать ли мышь
  9.     //-------------------------------------------------------//
  10.  
  11.     //-------------------------------------------------------//
  12.     public GameObject camera;                                        // ГО используемой камеры
  13.     public float      cameraMoveSpeedDamper = 0.02f;                 // коэффицент скорости перемещения мыши
  14.     public float      maxCamHeight;
  15.     private float     minCamHeight;
  16.     public float      mouseWheelSpeed = 150.0f;                      // скорость масштабирования поля колесиком мышки
  17.     //-------------------------------------------------------//
  18.  
  19.  
  20.     void Start()
  21.     {
  22.        minCamHeight = Terrain.activeTerrain.SampleHeight(camera.transform.position);
  23.     }
  24.  
  25.     // Update is called once per frame
  26.     void Update()
  27.     {
  28.         minCamHeight = Terrain.activeTerrain.SampleHeight(camera.transform.position);
  29.         if (Input.GetButtonDown("Move Camera"))
  30.         {
  31.             mouseButton2DownPoint = Input.mousePosition;
  32.             dragMouse = true;
  33.         }
  34.  
  35.         if (Input.GetButtonUp("Move Camera"))
  36.         {
  37.             dragMouse = false;
  38.             mouseButton2UpPoint = Input.mousePosition;
  39.         }
  40.  
  41.         if (Input.GetButtonDown("Turn Camera"))
  42.         {
  43.             turnCamera = true;
  44.             turnButtonDownPoint = Input.mousePosition;
  45.         }
  46.  
  47.         if (Input.GetButtonUp("Turn Camera"))
  48.         {
  49.             turnCamera = false;    
  50.         }
  51.  
  52.         if (dragMouse)     // перемещать ли камеру
  53.         {
  54.  
  55.             Vector2 dragDifference =  mouseButton2DownPoint - (Vector2)(Input.mousePosition);     // на сколько юзер передвинул мышку
  56.             // устанавливаем вектору движения разницу между положениями мыши
  57.             Vector3 moveDirection = new Vector3(-dragDifference.x, 0, -dragDifference.y);
  58.             // Translating so move relative to where the camera currently is located
  59.             transform.Translate(moveDirection * cameraMoveSpeedDamper);
  60.         }
  61.  
  62.         if (turnCamera)    // поворачивать ли камеру
  63.         {
  64.             //transform.rotation *  
  65.             Vector2 turnDifference = turnButtonDownPoint - (Vector2)(Input.mousePosition);     // на сколько юзер повернул мышку
  66.             Vector3 turnDirection = new Vector3(-turnDifference.x, -turnDifference.x, -turnDifference.y);
  67.    
  68.             //Quaternion zrot =  Quaternion.AngleAxis(transform.rotation.eulerAngles.x + (-turnDifference.y * cameraMoveSpeedDamper * 10), Vector3.left);
  69.             transform.rotation = Quaternion.AngleAxis(transform.rotation.eulerAngles.y + (turnDirection.y * cameraMoveSpeedDamper), Vector3.up);
  70.         }
  71.  
  72.         float mouseWheel = Input.GetAxis("Mouse ScrollWheel");        
  73.        
  74.         //if (mouseWheel != 0)                                     // если пользователь крутил колесико мышки
  75.         {
  76.             float currentHeight =  camera.transform.position.y;
  77.             // изменить высоту камер основываясь на заданной скорости
  78.             currentHeight -= mouseWheel * mouseWheelSpeed * Time.deltaTime;
  79.             // установить высоту камеры, не переходя мин/макс значений
  80.             if (currentHeight <= (minCamHeight + 3)) currentHeight = Terrain.activeTerrain.SampleHeight(camera.transform.position) + 5;
  81.             if (!(currentHeight <= minCamHeight) && !(currentHeight >= maxCamHeight))
  82.             {
  83.                 transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
  84.                 camera.transform.position = new Vector3(camera.transform.position.x, currentHeight, camera.transform.position.z);
  85.             }
  86.         }
  87.  
  88.        
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement