problems with unit selection script

Right now I’m making a script for unit selection. It sets a unit’s select script to selected if it can, and instantiates a marquee tool if the user drags the mouse in any direction. It also moves the camera to center the selected unit.

The problem right now is that if you select a unit that is far away from the center of the screen, obviously the cursor leaves the selected unit as the camera moves to center it. I think this makes it so that the ray being shot from the camera is not hitting a selected object, and so it doesn’t instantiate the marquee tool. I tried putting a boolean in to make the ray only fire as soon as the mouse button is pressed, and not again until it is pressed again, but I think something in my logic is wrong. Should be a simple fix. Here is the code in question…

(pastebin)

	if (Input.GetMouseButton(0)  !sphere){
		//ray to find out what i'm clicking on
		var obj : GameObject;
		var didCast : boolean;
		var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		var whatHit : RaycastHit;
		if (!didCast){
			if (Physics.Raycast(ray, whatHit)){
				didcast = true;
			}
		}
		// if the ray hits an object that can selected, select it and make selection sphere
	 	if(whatHit.collider != null){
	 		if (whatHit.collider.tag == ("pickUp")){
	 			obj = whatHit.collider.gameObject;
 				obj.GetComponent(selectScript).selected = true;
 				if (Input.GetAxis("Mouse Y") || Input.GetAxis ("Mouse X")){
	 				var clone : GameObject;
	 				clone = Instantiate(sphereObj, obj.transform.position, Quaternion.identity);
					clone.GetComponent(sphereBehavior).follow = obj;
					clone.transform.localScale = Vector3.one * 0.1;
					sphere = clone;
	 			}
 			}				
		}else{
			if(whatHit.collider != null){
				if (whatHit.collider.tag != ("pickUp")){
					deselect = true;
				}
			}
		}
	}
}

Thanks in advance. :slight_smile:

EDIT: put in a pastebin for clearer reading…

http://pastebin.com/6GaRSYA6

There were a number of coding errors. The general concept is that you select something and move it around or whatever you do with it.

The basis of the selection process is to keep track of what you have selected and do something with it. Below Is a rewrite of this to keep the selected object. When you click it deselects that object and removes the sphere. (which I assume is a marker showing the selection) It then creates another sphere and selects a new object if it has the proper tag.

var sphere : GameObject;
var selectedObject : GameObject;

function Update(){
	if (Input.GetMouseButton(0)){
		//ray to find out what i'm clicking on
		var obj : GameObject;
		var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		var whatHit : RaycastHit;
		var didCast : boolean = Physics.Raycast(ray, whatHit);
		
		if(selectedObject){
			selectedObject.GetComponent(selectScript).selected = false;
			if(sphere) Destroy(sphere);
		}
						
		// if the ray hits an object that can selected, select it and make selection sphere
		if(didCast){
			if (whatHit.collider.tag == ("pickUp")){
				obj = whatHit.collider.gameObject;
				var script = obj.GetComponent(selectScript);
				if(script){
					script.selected = true;
					sphere = Instantiate(sphereObj, obj.transform.position, Quaternion.identity);
					sphere.GetComponent(sphereBehavior).follow = obj;
					sphere.transform.localScale = Vector3.one * 0.1;
				}
			}
		}
	}
}

thanks a ton! I wrote this around 3 am, so i’m not surprised to hear it had a lot of errors.

question though, how will i know if the ray hit an object than can be selected?

I really don’t get the point of selectedObject, why did you bother rewriting that? My script iterates through a list of objects that have been selected