- Home /
Raycast starts ignoring gameobject after a few seconds
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 1000))
{
switch (hit.collider.gameObject.tag)
{
case "HQ":
GetComponent<GUIscript>().Nothselected=false;
GetComponent<GUIscript>().Workselected=false;
GetComponent<GUIscript>().HQselected=true;
GetComponent<GUIscript>().SelectedBuilding=hit.collider.gameObject;
break;
case "Worker":
GetComponent<GUIscript>().Nothselected=false;
GetComponent<GUIscript>().HQselected=false;
GetComponent<GUIscript>().SelectedBuilding=hit.collider.gameObject;
GetComponent<GUIscript>().Workselected=true;
hit.collider.gameObject.GetComponent<UniteMove>().selected=true;
break;
case "Tile":
GetComponent<GUIscript>().Nothselected=true;
break;
}
}
why is it that after a few seconds Raycast starts ignoring all the workers no matter how many i spawn. it still doesn't ignore the HQ, but in case of the workers it goes through them and registers the tile. i don't ever mess with the collider or tag on worker. NOTE: the script does work for the first 2-3 seconds, and then it starts ignoring the worker.
Put Debug.Log() statements to see what is going on. Between lines 4 and 5, add:
Debug.Log(hit.collider.tag+", "+hit.collider.name);
The between lines 15 and 16 insert:
Debug.Log("Worker found");
it finds the worker just fine for the first few seconds, then it starts ignoring it and telling me i am clicking on the tile.
Check the objects colliders and layers. $$anonymous$$aybe something else in your project is changing them ?
So the issue is not with this code. If your Raycast() is reporting 'Tile', then this code can only process that as a 'Tile'. Next steps are 1) to verify the raycasts, and 2) verify the colliders. Use a Debug.DrawRay() and view your game in both Game view and Scene view while the game is running. Select some workers that the raycast are missing in the Hierarchy and take a look at the collider(s) on that game object. Have they been resized or disabled? Are you don't anything 'special' with the game objects or colliders such as using negative values for localScale?
the raycasts are fine, the inspector says the capsule colider is fine, but here is the thing, i never do anything with the colliders, and yet even Physics.OverlapSphere fails to register the existence of the capsule coliders... what bugs me the most is that after the few seconds mark all the capsule colliders on all the workers fail to be registered, even though the inspector says everything is alright with them... If anything i think the Unity AI might have something to do with this (vague recollection tells me this started happening when i implemented the AI). here's the code for the AI if it helps any:
public class Unite$$anonymous$$ove : $$anonymous$$onoBehaviour {
// Use this for initialization
void Start () {
}
public bool selected=false;
// Update is called once per frame
void Update () {
if (selected)
{
if (Input.Get$$anonymous$$ouseButtonUp(1))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 1000))
{
if(hit.collider.gameObject.tag=="Tile")
{
GetComponent<Nav$$anonymous$$eshAgent>().destination=new Vector3(hit.point.x,transform.position.y,hit.point.z);
}
}
}
}
}
}