- Home /
Question by
CrimsonOrchid · Apr 17, 2020 at 09:19 AM ·
gameobjectraycastsecondsmaximizeignore objects
Raycast ignoring GameObjects on Maximize
So, I've been having various issues with an RTS-like unit selection system, where you click an unit to select it, and if you click the ground all units are deselected; after almost two hours testing stuff and trying to solve bugs I found that there's only one problem, and it only occurs on when the game window is on maximize. Basically, after a few seconds (something like 4.8 seconds to be more precise) the Raycast starts ignoring collision with some GameObjects (the player units, in this case)
public GameObject selectedObject;
public ObjectInfo selectedInfo;
public void LeftClick()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Ground")
{
selectedObject = null;
//deselect both units when clicking on ground
GameObject.Find("Hero").GetComponent<ObjectInfo>().isSelected = false;
GameObject.Find("Builder").GetComponent<ObjectInfo>().isSelected = false;
Debug.Log("Deselected");
}
if (hit.collider.tag == "Selectable")
{
selectedObject = hit.collider.gameObject;
selectedInfo = selectedObject.GetComponent<ObjectInfo>();
selectedInfo.isSelected = true; //this variable is used to determine wheter the unit can move or not in another script
Debug.Log("Selected " + selectedInfo.objectName);
}
}
}
Comment