Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges