• 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 /
  • Help Room /
avatar image
0
Question by izzanfuad · May 19 at 11:04 AM · guiraycastrts

My Drag Selection is not working!! Need Help for an RTS Game!

Hi, is there anyone that knows why my selection in bounds not working??? The Rect GUI when dragging have displayed but it won't select my units

I am using this tutorial: How To Build an RTS Game

and

Here's the code

     public static Texture2D WhiteTexture
     {
         get
         {
             if (_whiteTexture == null)
             {
                 _whiteTexture = new Texture2D(1, 1);
                 _whiteTexture.SetPixel(0, 0, Color.white);
                 _whiteTexture.Apply(); 
             }
 
             return _whiteTexture;
         }
     }
 
     public static void DrawScreenRect(Rect rect, Color color)
     {
         GUI.color = color;
         GUI.DrawTexture(rect, WhiteTexture);
     }
 
     public static void DrawScreenRectBorder(Rect rect, float thickness, Color color)
     {
         //Top
         DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
         //Bottom
         DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
         //Left
         DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
         //Right
         DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
     }
 
     public static Rect GetScreenRect(Vector3 screenPos1, Vector3 screenPos2)
     {
         screenPos1.y = Screen.height - screenPos1.y;
         screenPos2.y = Screen.height - screenPos2.y;
 
         Vector3 bR = Vector3.Max(screenPos1, screenPos2);
         Vector3 tL = Vector3.Min(screenPos1, screenPos2);
 
         return Rect.MinMaxRect(tL.x, tL.y, bR.x, bR.y);
     }
 
     public static Bounds GetVPBounds(Camera cam, Vector3 screenPos1, Vector3 screenPos2)
     {
         Vector3 pos1 = cam.ScreenToViewportPoint(screenPos1);
         Vector3 pos2 = cam.ScreenToViewportPoint(screenPos2);
 
         Vector3 min = Vector3.Min(pos1, pos2);
         Vector3 max = Vector3.Max(pos1, pos2);
 
         min.z = cam.nearClipPlane;
         max.z = cam.farClipPlane;
 
         Bounds bounds = new Bounds();
         bounds.SetMinMax(min, max);
 
         return bounds;  
     }
 
     Update()
     {
             if(Input.GetMouseButtonDown(0))
             {
                 mousePos = Input.mousePosition;
 
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);        
 
                 if(Physics.Raycast(ray, out hit))
                 {
                     LayerMask layerHit = hit.transform.gameObject.layer;
 
                     switch (layerHit.value)
                     {
                         case 9:
                             if(!selectedUnits.Contains(hit.transform))
                             SelectUnit(hit.transform);
                             break;
                         default:
                             DeselectUnit();
                             isDragging = true;
                             break;
                     }
                 }        
             }
 
             if(Input.GetMouseButtonUp(0))
             {
                 foreach(Transform child in map)
                 {
                     if(child.name == "3D Markers")
                     foreach(Transform units in child)
                     {
                         if(IsWithinSelectionBounds(units))
                         SelectUnit(units);
                     }
                 }
                 isDragging = false;
             }  
     }  
     
     private void OnGUI()
     {
         if (isDragging)
         {
             Rect rect = GetScreenRect(mousePos, Input.mousePosition);
             DrawScreenRect(rect, new Color(0f, 0f, 0f, 0.25f));
             DrawScreenRectBorder(rect, 3, Color.blue);
         }
     }
 
     private bool IsWithinSelectionBounds(Transform tf)
     {
         if(isDragging)
         {
             return false;
         }
 
         Camera cam = Camera.main;
         Bounds vpBounds = GetVPBounds(cam, mousePos, Input.mousePosition);
         return vpBounds.Contains(cam.WorldToViewportPoint(tf.position));
     }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by izzanfuad · May 19 at 07:49 AM

NEVERMIND, I ONLY FORGOT TO PUT AN EXCLAMATION MARK!!! I FEEL SO STUPID

      private bool IsWithinSelectionBounds(Transform tf)
      {
          if(isDragging)
          {
              return false;
          }
  
          Camera cam = Camera.main;
          Bounds vpBounds = GetVPBounds(cam, mousePos, Input.mousePosition);
          return vpBounds.Contains(cam.WorldToViewportPoint(tf.position));
      }
Comment
Add comment · Share
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

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

262 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

real time strategy (rts) single unit selection help 1 Answer

First Person Narrative Coding 2 Answers

Offset between mouse cursor position and "hit" position with worldspace UI's 1 Answer

I am trying to close the GUI if its already active and the user clicks 2 Answers

How to physics raycast through overlay UI? 1 Answer


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