Question by
mstachowsky · Oct 10, 2019 at 03:21 PM ·
spherecast
Spherecast only works twice
I am working with the basic roll-a-ball game. I want to add functionality where, if the player clicks near, but not necessarily on, one of the objects in the scene, something else happens. I am using SphereCast
to do this, but for some reason it only works twice. In the code below, I can click on any object in the scene and see its tag correctly. I then click on another object with a distinct tag and see that one's tag as well. However, I can't do that again - if I click on any object with a distinct tag, I still see the last clicked object's tag.
This code runs in a distinct game object called "MouseHandler", which is empty but runs the script below, which is really just its Update()
function
Any thoughts?
// Update is called once per frame
void Update()
{
//get a ray pointing along the mouse direction
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.SphereCast(ray, 0.1f, out hit) == true)
{
count++;
Debug.Log(hit.collider.tag);
}
}
}
Comment