when I have this version of code:
void Update()
{
RaycastHit hit = new RaycastHit();
for (int i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase.Equals(TouchPhase.Ended))
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast(ray, out hit))
{
hit.transform.gameObject.SendMessage("IncrementCounter");
}
}
}
}
it works fine and after one-click the counter is 1, after two-click the counter is 2 and it's ok.
I want to use FixedUpdate() instead of Update() because in my opinion Update() is too slow. In another words input is reading too slow - when I want to click three times very fast, a counter is increment too slow.
I tried to use FixedUpdate(), but I got bugs -> after one-click, counter is equal to three or sometimes two.
Any ideas ?
That is simply the nature of how FixedUpdate, Update, and Input interact. In your situation, the game loop could look like this:
-- Unity resets Input.touchCount -- Unity reads touch input, detects a touch, and sets Input.touchCount to 1 -- Unity determines that Physics simulation is 3 frames behind, so fires --- FixedUpdate - your code increments the counter --- FixedUpdate - your code increments the counter --- FixedUpdate - your code increments the counter -- Physics is now caught up so Unity fires --- Update
Because iOS devices are locked into vsync, you have a hard limit on how quickly you can process input (60fps - once every ~16ms). If you have multiple FixedUpdates running per Update you're probably not hitting 60fps so you can start there -- if you are hitting 60fps and it's still too slow then you have problems.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.