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