- Home /
Input loss. One out of every five or six mouse clicks or button presses don't register.
I've been trying for days with several different methods to make it work on EVERY click and it just won't work no matter which way I do it. I'm hoping someone can tell me what it is that I'm doing wrong.
I'm trying to instantiate a point effector on touch or click. It all works the way it's supposed to except that every couple of inputs nothing happens and nothing gets instantiated.
public class TouchForce: MonoBehaviour
{
public float Power = 10f;
public float Radius = 5f;
public float delay;
public GameObject obj;
void FixedUpdate ()
{
if (Input.GetButtonDown("Fire1"))
{
Invoke("Detonate",0);
}
}
void Detonate()
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = -10;
Vector3 objPos1 = Camera.main.ScreenToWorldPoint(mousePos);
GameObject explosionInstance = (GameObject)Instantiate(obj, objPos1, Quaternion.identity);
Destroy(explosionInstance, delay);
}
}
Any help would be greatly appreciated!
Have you tried just calling detonate without invoking it (since your delay is 0 anyway it shouldn't matter)?
Answer by Eno-Khaon · Apr 26, 2018 at 09:12 PM
You really don't want to implement Input into FixedUpdate().
Input is processed during Update(). To give an example of where your problems are coming from, let's assume this scenario for a moment:
Let's say you've set your physics update rate to 1 FixedUpdate() per second (or, by extension, a physics timestep of 1). If your main framerate is 60, then this leaves a 1/60 chance (effectively) that your timing is correct such that, when FixedUpdate() is processed, the state for GetButtonDown() will still be "down".
What I would recommend is to move your Input management into Update() while leaving any physics-related interactions in FixedUpdate().
Since nothing in the script example you provided should actually require physics, it may be as simple as removing the prefix "Fixed".
Edit: To demonstrate how the behavior works, here's another example showing how it could fail:
// Successfully detects GetButtonDown in FixedUpdate()
Update() // GetButtonDown
FixedUpdate() // (still down when checked)
Update() // GetButton (held)
Update() // GetButton (held)
FixedUpdate() // (held when checked)
// ...
// Failure to detect GetButtonDown in FixedUpdate()
Update()
FixedUpdate()
Update() // GetButtonDown
Update() // GetButton (held)
FixedUpdate() // (held when checked, no longer down)
Edit 2: fixed a few typos