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 Middleman3 · Jul 17, 2012 at 07:28 AM · rtsselectionclick and dragselecting

Click and Drag RTS Mouse selecting script help.

Alright. Hey everybody reading this, I'm working on my first RTS project, and I'm having trouble with a piece of code that is pretty complex to me. I'm using raycasts to select objects with the click of a mouse. Not so bad at first, and the typical selecting is no biggie. And by that I mean I can get the console to acknowledge the unit I clicked on. Baby steps.

However, I'm trying to pull of that thing where you click one spot, then move the mouse a bit, and release the mouse button, and the distance between the two clicks becomes a radius in which every unit inside becomes selected. I read a post on the forums and got the Idea of using a Physics.CapsuleCastAll. I'm pretty confused on how I should order everything to get it to work the way I want it to.

If anyone would be so kind as to point all my stupid mistakes, give advice on a better way to set it up, and/or maybe give me a hit on where I can go next to actually get a unit to stay selected until I hit a key or select another unit.

Any help with this would be vastly appreciated, by me and future RTS game makers with the same ambitions.

Here's the script. You'll see some things are empty cuz, Im not done of course.

 #pragma strict
 
 static var radius = startClick.magnitude - endClick.magnitude;
 static var hit : RaycastHit;
 static var endClick : = hit.point;
 static var startClick = hitInfo.point;
 
 function Update () 
 {
  if(Input.GetMouseButtonDown(0))
  {
  var twoSecondsLater = Time.time + 2;
  
  if(Time.time < twoSecondsLater)
  {
 
  
  
  }
  }
 
  if(Input.GetMouseButtonUp(0))
  {
  public var hitInfo : RaycastHit;
  
  if(Input.mousePosition)
  {
  var ray = transform.position;
  
  if(Physics.CapsuleCastAll(ray,Vector3(ray.x, ray.y + 10, ray.z), radius, transform.forward, hitInfo, Mathf.Infinity))
  {
  Selected ();
  }
  }
  } 
 }
 
 
 
 function DefaultSelect ()
 {
  var ray : Ray = camera.ScreenPointToRay(Input.mousePosition);
  
  if(Physics.Raycast(ray, hit, Mathf.Infinity))
  {
  Selected();
  }
  else 
  {
  print ("Did not Hit");
  }
 }
 
 function Selected ()
 {
  var selectedUnit = hit.transform;
  
  Debug.DrawRay (transform.position, transform.TransformDirection (Vector3.forward) * hit.distance, Color.yellow);
  print ("Did Hit");
  
  Debug.Log(hit.collider.gameObject.name + " Selected");
 }
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 asafsitner · Jul 17, 2012 at 07:56 AM

Lose the capsule cast.

If you know the world position where you clicked the mouse button, and the world position where you released the mouse button, you have a 2D rectangle. You can compare units positions to figure out which units are inside that rectangle, possibly even create a bounding box (with arbitrary height to compensate for terrain etc.) and check against that using `Bounds.Contains`.

Comment
Add comment · Show 8 · 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
avatar image Middleman3 · Jul 17, 2012 at 09:05 PM 0
Share

Thanks bro. :)

avatar image asafsitner · Jul 17, 2012 at 11:31 PM 0
Share

Hey, no problem. Let me know how it worked out. :)

avatar image Middleman3 · Aug 08, 2012 at 06:48 PM 0
Share

Worked out smart. :)

avatar image IndieScapeGames · Aug 08, 2012 at 06:54 PM 0
Share

Could you update your code $$anonymous$$iddleman3 to help others find the answer you came to?

avatar image yveris · Nov 21, 2012 at 11:59 PM 0
Share

I'm a bit confused, I understand you create your rectangle and now you have a bounds. How do you check whether the units are within that bounds? With ray casts or do you have to check every single object in the scene to see if they are inside Bounds.Contains?

Show more comments

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

7 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# converting Input.Mouseposition.y to to "top" argument of GUI.Box 1 Answer

Unity RayCast Selection 1 Answer

How to make free area selection(like lasso tool in photoshop) in rts? 2 Answers

Strange Behavior with RTS-Style Selection Box and Moving Camera 0 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