Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 14th, 2012  |  syntax: JavaScript  |  size: 4.60 KB  |  hits: 27  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function Update ()
  2. {
  3.         if (Input.GetButtonDown("Fire1"))
  4.         {
  5.                 mouseButton1DownPoint = Input.mousePosition;
  6.                
  7.                 var hit1 : RaycastHit;
  8.                 var ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
  9.                 //Debug.DrawRay (ray.origin, ray.direction * 100.0, Color.green);
  10.                 if ( Physics.Raycast (ray1, hit1, raycastLength) ) // terrainLayerMask
  11.                 {
  12.                         if (hit1.collider.name == "Terrain")
  13.                         {
  14.                                 print ("Mouse Down Hit Terrain " + hit1.point);
  15.                                 mouseButton1DownTerrainHitPoint = hit1.point;
  16.                                
  17.                                 leftRectStart = hit1.point;
  18.                                 lineRenderer.SetPosition(0, leftRectStart);
  19.                                 lineRenderer.SetPosition(1, leftRectEnd);
  20.                                 mouseLeftDrag = true;
  21.                         }
  22.                         else
  23.                         {
  24.                                 print ("Mouse Down Hit something: " + hit1.collider.name);
  25.                                 //hit.collider.gameObject.SendMessage("SetSelected");
  26.                                 // Ray hit a unit, not the terrain. Deselect all units as the fire 1 up
  27.                                 // event will then select that just recently clicked unit!
  28.                                 ClearSelectedUnitsList();
  29.                         }
  30.                         //Debug.DrawRay (ray.origin, ray.direction * 100.0, Color.green);      
  31.                 }
  32.         }
  33.        
  34.         if (Input.GetButtonUp("Fire1"))
  35.         {
  36.                 mouseButton1UpPoint = Input.mousePosition;
  37.                 print("units? " + unitManager.GetSelectedUnitsCount());
  38.                
  39.                 if (mouseButton1DownPoint == mouseButton1UpPoint) {
  40.                         // user just did a click, no dragging. mouse 1 down and up pos are equal.
  41.                         // if units are selected, move them. If not, select that unit.
  42.                         if (unitManager.GetSelectedUnitsCount() == 0) {
  43.                                 // no units selected, select the one we clicked - if any.
  44.                                 ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
  45.                                 if ( Physics.Raycast (ray1, hit1, raycastLength, nonTerrainLayerMask) )
  46.                                 {
  47.                                         // Ray hit something. Try to select that hit target.
  48.                                         //print ("Hit something: " + hit.collider.name);
  49.                                         hit1.collider.gameObject.SendMessage("SetSelected");
  50.                                 }
  51.                                                                
  52.                         }
  53.                        
  54.                 } else {
  55.                         // mouse is dragged
  56.                         ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
  57.                        
  58.                         if ( Physics.Raycast (ray1, hit1, raycastLength, terrainLayerMask) )
  59.                         {
  60.                                 //if (hit.collider.name == "Terrain")
  61.                                 //{
  62.                                         print ("Hit Terrain 2 " + hit1.point);
  63.                                         //MoveSelectedUnitsToPoint(hit.point);
  64.                                         leftRectEnd = hit1.point;
  65.                                         ClearSelectedUnitsList();
  66.                                         SelectUnitsInArea(leftRectStart, leftRectEnd);
  67.                                
  68.                                         lineRenderer.SetPosition(1, leftRectEnd);
  69.                         }
  70.                 }
  71.         }
  72.        
  73.         //if (mouseLeftDrag) {
  74.         //}
  75.        
  76.        
  77.         // Debug rays for selection rect
  78.         Debug.DrawRay(leftRectStart, Vector3.up * 10, Color.red);
  79.         Debug.DrawRay(leftRectEnd, Vector3.up * 10, Color.red);
  80.         //WorldToScreenPoint
  81.        
  82.        
  83.         if (Input.GetButtonDown("Fire2"))
  84.         {
  85.                 mouseButton2DownPoint = Input.mousePosition;
  86.                 mouseRightDrag = true;
  87.                
  88.         }
  89.        
  90.         if (Input.GetButtonUp("Fire2"))
  91.         {
  92.                 mouseRightDrag = false;
  93.                 mouseButton2UpPoint = Input.mousePosition;
  94.                 //if (mouseButton2DownPoint == mouseButton2UpPoint) {
  95.                         //ClearSelectedUnitsList();
  96.                 //}
  97.                
  98.                 var hit2 : RaycastHit;
  99.                 var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
  100.                 //Debug.DrawRay (ray.origin, ray.direction * 100.0, Color.green);
  101.                 if ( Physics.Raycast (ray2, hit2, raycastLength) ) // terrainLayerMask
  102.                 {
  103.                         if (hit2.collider.name == "Terrain")
  104.                         {
  105.                                 print ("Mouse Down Hit Terrain " + hit2.point);
  106.                                
  107.                                 //MoveSelectedUnitsToPoint(hit.point);
  108.                                 mouseButton2DownTerrainHitPoint = hit2.point;
  109.                         }
  110.                 }
  111.                
  112.                 // untis are selected, move them. Unit Manager's unit count is > 0!
  113.                 MoveSelectedUnitsToPoint(mouseButton2DownTerrainHitPoint);
  114.         }
  115.        
  116.         if (mouseRightDrag)
  117.         {
  118.                
  119.                 var dragDifference : Vector2 = mouseButton2DownPoint - Input.mousePosition;
  120.                 //print("Mouse drag distance: " + dragDifference.x + " " + dragDifference.y);
  121.                
  122.                 dragDifference /= 40.0;
  123.                
  124.                 moveDirection = new Vector3(-dragDifference.x, 0, -dragDifference.y);
  125.                 // Translating so move relative to where the camera currently is located
  126.                 transform.Translate(moveDirection);
  127.                 var speed = 1.0;
  128.                 moveDirection *= speed;
  129.  
  130.                 // Move the controller
  131.                 var controller : CharacterController = GetComponent(CharacterController);
  132.                 controller.Move(moveDirection * Time.deltaTime);
  133.         }
  134.        
  135.         var mouseWheelSpeed : float = 150.0;
  136.         var mouseWheel : float = Input.GetAxis ("Mouse ScrollWheel");
  137.         if (mouseWheel != 0)
  138.         {
  139.                 //print("mouse wheel value: " + mouseWheel);
  140.                 var currentHeight = transform.position.y;
  141.                 currentHeight -= mouseWheel * mouseWheelSpeed * Time.deltaTime;
  142.                 if (currentHeight > 150.0) {
  143.                         currentHeight = 150.0;
  144.                 }
  145.                 if (currentHeight < 20.0) {
  146.                         currentHeight = 20.0;
  147.                 }
  148.  
  149.                 transform.position = Vector3(transform.position.x, currentHeight, transform.position.z);
  150.         }
  151. }