- Home /
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?
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.
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? "
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
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.
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);
}
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
Follow this Question
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