- Home /
Rts people select
Im trying to help a friend create a RTS game and i want to program it so i can create a box by clicking down on my mouse and dragging it and what ever is in the box gets selected this is what is have so far
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RTStargeting : MonoBehaviour {
public Transform selectedTarget;
void Update(){
if (Input.GetMouseButtonUp(0)){ // when button clicked...
GUI.Box(new Rect());
}
private void SelectTarget(){
selectedTarget.renderer.material.color = Color.red;
}
private void DeselectTarget(){
if (selectedTarget){ // if any guy selected, deselect it
selectedTarget.renderer.material.color = Color.blue;
selectedTarget = null;
}
}
} i have no idea what to put in the GUI box can anyone help me alter or complete this script so that when i click down and darg my mouse it creates a box and whatever character that are in it get selected
@Will467, don't ask people to write scripts for you - that's a recipe for disaster here in UA, and you will only get downvotes. Try something like "could someone indicate a tutorial" or similar things - you may even end with a C# script written by some good soul, or at least with several useful links.
Oh ok im still new to unity and program$$anonymous$$g i didnt know ill alter my question.
ok i altered my question, also i wanted to thank you for all your help it is really appreciated
Answer by Adamcbrz · Apr 07, 2012 at 04:56 AM
Well you need to find your two points in the world to draw your box. I would probable setup a plane with a collider that represents your ground and raycast from your mouseposition using the camera to ray function ( on phone so can't look up docs ). Then store the raycast hit point into a vector3 on mouse down then while the mouse is down update you box to draw in world space from your starting vector3 to your current mouse position hit point. Once you define that area all you have to do is determine which Targets are in that area.
Hope that helps.
how would i program that i am a newbie at program$$anonymous$$g and i have no idea how i could program that
Would you use a ray cast from the selection box, like iterate through each point within the box? I have a function that creates the selection box, but I'm unsure whether to use ray casts or create a box and use a sweep test.
But I assume the sweep test would have have too many collisions once there's many units and impact performance.
Answer by Malevolent_King · Apr 24, 2012 at 08:10 PM
http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html
find mouseposition using raycast