- Home /
Help - Selection System
The game that I am making right now involves the player selecting an object in the scene , and then being able to manipulate it in many different ways. I already have a (kind of) working selection script, but it feels really bad and wrong. Basically, what happens is, when the player hits the spacebar, it selects the object that is lined up with a crosshair in the middle of the view (raycasting). The way that I show the player that the gameobject is selected is by changing it's material Color to blue instead of white. I then set a variable (selObj) to be the selected object. Is there a better way to do this? I am also having trouble with deleting the selObj too, but I think that this is because of the selection method.
Here is my current script:
var selObj : Transform; var oldObj : Transform;
var fwd = transform.TransformDirection (Vector3.forward);
var hit : RaycastHit;
if(Input.GetKeyUp(KeyCode.Space)) { if (Physics.Raycast (transform.position, fwd, hit)) { if(hit.transform.gameObject.tag != "NotSelectable") { oldObj.transform.renderer.material.color = Color.white;
if(hasSelector == false)
{
selObject = hit.transform;
}
selObj = hit.transform;
oldObj = hit.transform;
selObj.renderer.material.color = Color.blue;
}
}
Thanks in advance,
Xedfire
"I already have a (kind of) working selection script, but it feels really bad and wrong." - Why don't you post it here then and see what we can do about it?
"Is there a better way to do this?" - Eh, it's hard to tell without knowing what it is you want to do better. :)
I added the script - it should work, but I had to re-order a few things
Answer by Statement · Mar 29, 2011 at 04:07 PM
You know, I'd create two scripts for this. One that handles the selection and one that handles coloring upon selection. See the two scripts below.
Selecter.js
var selected : Transform; static var unrequired = SendMessageOptions.DontRequireReceiver;
function Update() { if (Input.GetKeyUp(KeyCode.Space)) { var hit : RaycastHit; if (Physics.Raycast(transform.position, transform.forward, hit) && hit.transform.tag != "NotSelectable") { Select(hit.transform);
} } }
function Select(other : Transform) { if (selected) selected.SendMessage("OnSelected", false, unrequired); selected = other; if (selected) selected.SendMessage("OnSelected", true, unrequired); }
SelectedColor.js
OnSelected(false);
function OnSelected(selected : boolean) { renderer.material.color = selected ? Color.blue : Color.white; }
One advantage is that by using SendMessage we can notify each individual object that they are selected or not, so they can apply whatever logic they want when they are selected. You could even make some objects play a sound or change to a custom color of their own.
One disadvantage is that you'd have to put SelectedColor on each selectable item so if you have a lot of them it just becomes annoying to set up. In that case you might want to just do the material color change right in Selecter.
This solution is very like mine in Spotlight switch script? question, have a look there as well.