hello im having a problem, my script doesnt seem to work, its suppose to send a raycast from the mousepointer and if it hits a collider with the tag “TownHall” it would tell me in the console but it doesnt work. The tag is correct and has a box collider on it, any idea why its not working?
Thanks
function Update ()
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, hit, 1000))
{
if (hit.collider.gameObject.tag == "TownHall")
{
Debug.Log("HIT TOWNHALL");
}
}
}
}
Hi! just guessing, aren’t you missing the direction?
Raycast(origin: Vector3, direction: Vector3, maxDistance: float = Mathf.Infinity)
or something like this:
Raycast(origin: Vector3, direction: Vector3, out hitInfo: RaycastHit, maxDistance)
with the out word…
the code could be like…
function Update ()
{
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var fwd: Vector3 = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(ray, fwd, out hit, 1000))
{
if (hit.collider.gameObject.tag == "TownHall")
{
Debug.Log("HIT TOWNHALL");
}
}
}
}
sorry, not time for test, hope this helps! good luck!