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…
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.
EDIT: put in a pastebin for clearer reading…