- Home /
 
 
               Question by 
               mushiepang · Mar 19, 2012 at 05:34 AM · 
                rtsselectionunits  
              
 
              unit selection issue
Hey, im having issues with my unit selection program, the code says there is miss used symbols and suth, anyone help?
using UnityEngine; using System.Collections;
public class selection : MonoBehaviour {
  //the mouse position when the user move it ("bottom right" corner of the rect if you start from left to right and from top to bottom)
  
   private var mouseButton1DownPoint = Vector2;
   
   //the mouse position where the user first click ("top left" corner of the rect if you start from left to right and from top to bottom)
   
   private var mouseButton1UpPoint = Vector2;
   
 
   
   //the world position of the 4 corners
   
   private var topLeft = Vector3;
   
   private var topRight = Vector3;
   
   private var bottomLeft = Vector3;
  
   private var bottomRight = Vector3;
  
    
  
   //the pointer moved where the user right-click (for moving unit(s))
  
   var target = Transform;
  
    
  
   //the list of selected units
  
   private var listSelected = new Array ();
  
    
  
   //the list of ALL units in the scene
  
   private var listAllUnits = new Array ();
  
    
  
   private var hit = RaycastHit;
  
    
  
   //boolean to know if the left mouse button is down
  
   private var leftButtonIsDown = (boolean = false);
  
    
  
   //cube placed at the 4 corner of the polygon, for visual debug
  
   private var topLeftCube;
  
   private var bottomRightCube;
  
   private var topRightCube;
  
   private var bottomLeftCube;
  
    
  
   //the layer mask for walkable zones
  
   private  var layerMask = 1 << 9;
  
   //the layer mask for selectable objects
  
  private var selectableLayerMask = 1 << 10;
  
    
  
   //width and height of the 2D rectangle
  
   var width = int;
  
   var height = int;
  
    
  
   var debugMode = boolean = false;
  
    
  
   var selectionTexture = Texture;
  
    
  
   // range in which a mouse down and mouse up event will be treated as "the same location" on the map.
  
   private var mouseButtonReleaseBlurRange = (int = 0);
  
    
  
   function OnGUI() {
  
       if (leftButtonIsDown) {
  
          
  
           width = mouseButton1UpPoint.x - mouseButton1DownPoint.x;
  
           height = (Screen.height - mouseButton1UpPoint.y) - (Screen.height - mouseButton1DownPoint.y);
  
           var rect = (Rect = Rect(mouseButton1DownPoint.x, Screen.height - mouseButton1DownPoint.y, width, height));
  
           GUI.DrawTexture (rect, selectionTexture, ScaleMode.StretchToFill, true);       
  
          
  
       }
  
   }
  
    
  
    
  
   function Start()
  
   {
  
      
  
       if(debugMode)
  
       {
  
           topRightCube = GameObject.Find("topRight").transform;   
  
           bottomLeftCube = GameObject.Find("bottomLeft").transform;   
  
           topLeftCube = GameObject.Find("topLeft").transform; 
  
           bottomRightCube = GameObject.Find("bottomRight").transform;
  
       }
  
      
  
   }
  
    
  
   function Update () {
  
    
  
       if (Input.GetButtonDown ("Fire1"))
  
       {
  
           //if left button is down, save the mouse position, set the leftButtonIsDown to true and save the world position of the rectangle's "top left" corner
  
           mouseButton1UpPoint=Input.mousePosition;
  
           leftButtonIsDown = true;
  
           Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),  hit, 1000);
  
           topLeft = hit.point;       
  
       }
  
      
  
       if (Input.GetButtonUp ("Fire1"))
  
       {
  
           //if left button is up set the leftButtonIsDown to false
  
           leftButtonIsDown = false;
  
          
  
           //if the range is not big enough, it's a simple clic, not a dragg-select operation
  
           if (IsInRange(mouseButton1DownPoint, mouseButton1UpPoint))
  
           {      
  
               // user just did a click, no dragging. mouse 1 down and up pos are equal.
  
               // if units are selected, move them. If not, select that unit.
  
               if (GetSelectedUnitsCount() == 0)
  
               {
  
                   // no units selected, select the one we clicked - if any.
  
                  
  
                   if ( Physics.Raycast (Camera.main.ScreenPointToRay (mouseButton1DownPoint), hit, 1000, selectableLayerMask) )
  
                   {
 
                       // Ray hit something. Try to select that hit target.
 
                       //print ("Hit something: " + hit.collider.name);
 
                       AddSelectedUnit(hit.collider.gameObject);
 
                   }
 
                                  
 
               }
 
           }
 
          
 
       }
 
      
 
       if (Input.GetButtonUp ("Fire2"))
 
       {
 
           //right click, move the pointer to the position
 
           if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),  hit, 1000,layerMask))
 
           {      
 
               target.position=hit.point;
 
               target.position.y=0;
 
               //todo : move the selected units to the position of the pointer.
 
           }
 
          
 
      
 
       }
 
      
 
       //if the left button is down and the mouse is moving, start dragging
 
      
 
       if(leftButtonIsDown)
 
       {
 
           //actual position of the mouse
 
           mouseButton1DownPoint=Input.mousePosition;
 
          
 
           //set the 3 other corner of the polygon in the world space
 
          Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),  hit, 1000);
 
           bottomRight = hit.point;
 
                  
 
           Physics.Raycast(Camera.main.ScreenPointToRay(Vector2(Input.mousePosition.x+width,Input.mousePosition.y)),  hit, 1000);
 
           bottomLeft= hit.point;
 
                  
 
           Physics.Raycast(Camera.main.ScreenPointToRay(Vector2(Input.mousePosition.x,Input.mousePosition.y-height)),  hit, 1000);
 
           topRight= hit.point;
          
 
           ClearSelectedUnitsList();      
 
          SelectUnitsInArea();
 
          
 
           if(debugMode)
 
           {
 
               bottomRightCube.position = bottomRight;
 
               topRightCube.position = topRight;
 
               bottomLeftCube.position = bottomLeft;
 
               topLeftCube.position = topLeft;    
 
           }
 
          
 
       }
 
          
 
          
 
          
 
    
 
   }
 
    
 
    
 
    
   function AddSelectedUnit(unitToAdd  GameObject) {
 
       listSelected.Push(unitToAdd);
 
       unitToAdd.GetComponent(pathTest).setSelectCircleVisible(true); 
 
   }
 
    
 
   function ClearSelectedUnitsList() { 
 
      
 
       for (var unitToClear  GameObject in listSelected) {
 
           unitToClear.GetComponent(pathTest).setSelectCircleVisible(false);   
 
       
 
       listSelected.Clear();
 
   
 
    
 
   function fillAllUnits(unitToAdd : GameObject)
 
   {
 
       listAllUnits.Add(unitToAdd);
 
   
 
    
 
   function SelectUnitsInArea() {
 
       var poly = new Array();
 
      
 
       //set the array with the 4 points of the polygon
 
       poly[0] =  topLeft;
 
       poly[1] = topRight;
 
       poly[2] = bottomRight;
 
       poly[3] = bottomLeft;
 
      
 
       //iterate trough the all unit's array
 
       for (var go : GameObject in listAllUnits) {
 
           var goPos ( Vector3 = go.transform.position);
 
           //if the unit is in the polygon, select it. 
 
           if (isPointInPoly(poly, goPos))
 
           {
 
               AddSelectedUnit(go);
 
           }
 
       }
 
   }   
 
    
 
    
 
   //math formula to know if a given point is inside a polygon
 
   function isPointInPoly(poly, pt){
       var c = false;
 
        l = poly.length;
 
        j = l - 1;
 
        
 
       for(i = -1 ; ++i < l; j = i){      
 
           if(((poly[i].z <= pt.z && pt.z < poly[j].z) || (poly[j].z <= pt.z && pt.z < poly[i].z))
 
           && (pt.x < (poly[j].x - poly[i].x) * (pt.z - poly[i].z) / (poly[j].z - poly[i].z) + poly[i].x))
 
           c = !c;
 
           }
 
       return c;
 
   }
 
    
 
   function GetSelectedUnitsCount() {
 
       return listSelected.length;
 
   }
 
    
 
   function IsInRange(v1 : Vector2, v2 : Vector2) : boolean {
 
       var dist = Vector2.Distance(v1, v2);
 
      
 
       if (Vector2.Distance(v1, v2) < mouseButtonReleaseBlurRange) {
 
           return true;
 
       }
 
       return false;
 
   }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Unity RayCast Selection 1 Answer
rts developement 0 Answers
RTS Grid Idea? 1 Answer
Drag to make box in RTS? 3 Answers