Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by Liuxin · May 19, 2013 at 03:30 PM · rtsselection

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

How to make free area selection drawn by mouse(like lasso tool in photoshop) for objects in rts game?alt text

i can make selection box, but i dont know how to make this.

if you have ideas about making it, please tell me your amazing ideas.

thanks for your advice.

iwanna.png (7.8 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · May 19, 2013 at 09:20 PM

You can detect objects inside an arbitrary region in Unity with the following trick: create a mesh trigger with the desired shape, add a kinematic rigidbody and force collision calculation by "moving" the trigger to its own position: OnTriggerEnter events will be generated for each collider inside the trigger, and you can collect them in a list or set a flag in each object's script.

The example below generates a squared horizontal mesh collider measuring 5x5 and prints the names of all objects detected. Attach this script to an empty object placed at the center of the area you want to check:

 #pragma strict
 
 private var vertices: Vector3[] = [
     Vector3(-5,0,0),
     Vector3(0,0,5),
     Vector3(5,0,0),
     Vector3(0,0,-5)
 ];
 
 private var tris: int[] = [
     0,1,2,
     2,3,0
 ];
 
 function DetectRegion(){
     var mesh: Mesh = new Mesh(); // create a mesh...
     mesh.vertices = vertices; // with the desired shape
     mesh.triangles = tris;
     // add a mesh collider...
     var col: MeshCollider = gameObject.AddComponent(MeshCollider);
     col.isTrigger = true;
     col.sharedMesh = mesh;
     col.convex = true;
     // and a kinematic rigidbody
     var rb: Rigidbody = gameObject.AddComponent(Rigidbody);
     rb.isKinematic = true;
     // force collision calculation:
     rigidbody.position = transform.position;
     // wait next FixedUpdate...
     yield WaitForFixedUpdate();
     // and destroy the trigger
     Destroy(rb);
     Destroy(col);
     Destroy(mesh);
 }
 
 function OnTriggerEnter(other: Collider){
     // one OnTriggerEvent is generated for each object
     print(other.name);
 }
 
 function Update(){
     // detect objects inside region when D is pressed:
     if (Input.GetKeyDown("d")) DetectRegion();
 }

Now comes the worst part: track the mouse movement and generate a mesh with these points. This is a very complicated task, and I suggest you to post another question about this particular subject - something like "How to create an arbitrary horizontal mesh with the mouse? "

Comment
Add comment · Show 4 · 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 sparkzbarca · May 19, 2013 at 10:48 PM 0
Share

upvote for very complete answer

avatar image Bunny83 · Jun 07, 2013 at 01:30 PM 1
Share

I've accepted the answer ;)

avatar image Scribe · Jun 07, 2013 at 05:04 PM 1
Share

Thought I'd see what I could do with this, managed to get it working with the left mouse button. It only works for convex colliders still, not sure how one would go about splitting a concave mesh into several convex ones for trigger detection.

 private var edgeVerts : Vector3[];
 private var vertices : Vector3[];
 private var tris : int[];
 var pointSpacing = 5;
 private var mousePos : Vector3;
 private var edges = new Array();
 var maxDist = 100;
 
 function Update () {
     if(Input.Get$$anonymous$$ouseButtonDown(0)){
         edges.Clear();
         SaveEdge();
     }
     if(Input.Get$$anonymous$$ouseButton(0)){
         if((mousePos-Input.mousePosition).sqr$$anonymous$$agnitude >= pointSpacing*pointSpacing){
             SaveEdge();
         }
     }
     if(Input.Get$$anonymous$$ouseButtonUp(0)){
         edges.Add(Vector3.zero);
         tris = CreateTris();
         Create$$anonymous$$esh(edges, tris);
     }
 }
 
 function SaveEdge () {
     mousePos = Input.mousePosition;
     edges.Add(Camera.main.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, maxDist)));
 }
 
 function CreateTris () : int[] {
     var trisList = new System.Collections.Generic.List.<int>();
     for(var i = 0; i < edges.length; i++){
         trisList.Add(i);
         trisList.Add((i+1)%(edges.length-1));
         trisList.Add(edges.length-1);
     }
     return trisList.ToArray();
 }
 
 function Create$$anonymous$$esh (verts, tris) {
     var mesh: $$anonymous$$esh = new $$anonymous$$esh();
     mesh.vertices = verts;
     mesh.triangles = tris;
     
     var col: $$anonymous$$eshCollider = gameObject.AddComponent($$anonymous$$eshCollider);
     col.isTrigger = true;
     col.shared$$anonymous$$esh = mesh;
     col.convex = true;
     var rb: Rigidbody = gameObject.AddComponent(Rigidbody);
     rb.is$$anonymous$$inematic = true;
     rigidbody.position = transform.position;
     yield WaitForFixedUpdate();
     
     Destroy(rb);
     Destroy(col);
     Destroy(mesh);
 }
 
 function OnTriggerEnter(other: Collider){
     print(other.name);
 }

Sorry for hijaking your answer!

Scribe

avatar image aldonaletto · Jun 07, 2013 at 09:04 PM 0
Share

You're welcome, @Scribe! But I suggest you to add this code also as an answer to this question, so that it can be upvoted as well.

avatar image
0

Answer by morgan_heijdemann · Jul 11, 2014 at 06:27 PM

C# version (haven't tested it)

     Vector3[] edgeVerts;
     Vector3[] vertices;
     int[] tris;
     int pointSpacing = 5;
     Vector3 mousePos;
     List<Vector3> edges = new List<Vector3>();
     int maxDist = 100;
     
     void Update () {
         if(Input.GetMouseButtonDown(0)){
             //Array.Clear(edges, 0, edges.Length);
             edges.Clear();
             SaveEdge();
         }
         if(Input.GetMouseButton(0)){
             if((mousePos-Input.mousePosition).sqrMagnitude >= pointSpacing*pointSpacing){
                 SaveEdge();
             }
         }
         if(Input.GetMouseButtonUp(0)){
             edges.Add(Vector3.zero);
             tris = CreateTris();
             CreateMesh(edges.ToArray(), tris);
         }
     }
     
     void SaveEdge () {
         mousePos = Input.mousePosition;
         edges.Add(Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, maxDist)));
     }
     
     int[] CreateTris() {
         List<int> trisList = new List<int>();
         for(var i = 0; i < edges.Count; i++){
             trisList.Add(i);
             trisList.Add((i+1)%(edges.Count-1));
             trisList.Add(edges.Count-1);
         }
         return trisList.ToArray();
     }
     
     IEnumerator  CreateMesh (Vector3[] verts, int[] tris) {
         Mesh mesh = new Mesh();
         mesh.vertices = verts;
         mesh.triangles = tris;
         
         MeshCollider col = (MeshCollider) gameObject.AddComponent(typeof(MeshCollider));
         col.isTrigger = true;
         col.sharedMesh = mesh;
         col.convex = true;
         Rigidbody rb = (Rigidbody) gameObject.AddComponent(typeof(Rigidbody));
         rb.isKinematic = true;
         rigidbody.position = transform.position;
         yield return new WaitForFixedUpdate();
         
         Destroy(rb);
         Destroy(col);
         Destroy(mesh);
     }
     
     void OnTriggerEnter(Collider other){
         print(other.name);
     }


Comment
Add comment · Show 1 · 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 oliversitan · Apr 26, 2021 at 05:08 PM 1
Share

This is a fixed script on c#

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class scr_model_selector : MonoBehaviour
 {
     Vector3[] edgeVerts;
     Vector3[] vertices;
     int[] tris;
     [SerializeField] int pointSpacing = 5;
     Vector3 mousePos;
     List<Vector3> edges = new List<Vector3>();
     [SerializeField] int maxDist = 100;
 
     void Update()
     { 
         if (Input.GetMouseButtonDown(0)) {
             //Array.Clear(edges, 0, edges.Length);
             edges.Clear();
             SaveEdge();
         }
         if (Input.GetMouseButton(0)) {
             if ((mousePos - Input.mousePosition).sqrMagnitude >= pointSpacing * pointSpacing) {
                 SaveEdge();
             }
         }
         if (Input.GetMouseButtonUp(0)) {
             edges.Add(Vector3.zero);
             tris = CreateTris();
             StartCoroutine(
             CreateMesh(edges.ToArray(), tris));
         }
     }
 
     void SaveEdge()
     {
         mousePos = Input.mousePosition;
         edges.Add(Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, maxDist)));
     }
 
     int[] CreateTris()
     {
         List<int> trisList = new List<int>();
         for (var i = 0; i < edges.Count; i++) {
             trisList.Add(i);
             trisList.Add((i + 1) % (edges.Count - 1));
             trisList.Add(edges.Count - 1);
         }
         return trisList.ToArray();
     }
 
     IEnumerator CreateMesh(Vector3[] verts, int[] tris)
     {
         Debug.Log("CreateMesh");
         Mesh mesh = new Mesh();
         mesh.vertices = verts;
         mesh.triangles = tris;
 
         MeshCollider col = (MeshCollider)gameObject.AddComponent(typeof(MeshCollider));
         col.sharedMesh = mesh;
         col.convex = true;
         col.isTrigger = true;
 
 
         Rigidbody rb = (Rigidbody)gameObject.AddComponent(typeof(Rigidbody));
         rb.isKinematic = true;
         GetComponent<Rigidbody>().position = transform.position;
         yield return new WaitForFixedUpdate();
 
         Destroy(rb);
         Destroy(col);
         Destroy(mesh);
     }
 
     void OnTriggerEnter(Collider other)
     {
         print(other.name);
     }
 
 }



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

19 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

Related Questions

Drag to make box in RTS? 3 Answers

Click and Drag RTS Mouse selecting script help. 1 Answer

RTS Game: Unit selection through Raycast 2 Answers

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

RTS rectangle selection system 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