- Home /
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");
}
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`.
Could you update your code $$anonymous$$iddleman3 to help others find the answer you came to?
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?
Your answer
Follow this Question
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