- Home /
Input.GetMouseButtonDown only works for 10seconds
Hi all, I have a simple script to select a gameobject to be the selected unit. However, after 10 seconds or so - Input.GetMosueButtonDown doesnt work anymore.
I have put this method in Update(). Does anyknow what the problem can be? It works for 10 seconds and then it stops working.
public void RangedAttack()
{
if(Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.transform.tag == "Enemy")
{
selectedUnit = hit.transform.gameObject;
Debug.Log(hit);
}
}
}
}
I don't think the issue comes from GetMouseButtonDown
. Ensure RangedAttack
is called first of all.
But it works for the first 10 seconds, I get the hit info in console when I press the mousebutton on the target. After 10 seconds, it refuses to display the message again in console.
Answer by Pokedlg3 · Mar 31, 2021 at 03:02 AM
you can try to create a bool when you press the button, oplhe try like this:
public void RangedAttack()
{
if(Input.GetMouseButtonDown(1))
{
pressing = true;
}
else if(Input.GetMouseButtonUp(1))
{
pressing = false;
}
if(pressing == true)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
if(hit.transform.tag == "Enemy")
{
selectedUnit = hit.transform.gameObject;
Debug.Log(hit);
}
}
}
}
Answer by danny_appel · Mar 31, 2021 at 07:59 PM
Nevermind.. For some reason - it is fixed now? A day later with no real change, it works. Not sure what the problem was.