- Home /
Missing taps on triggers/collider
I'm trying to make a falling object fly back up when the user taps it in the lower part of the screen.
Rigidbody2D rb;
void Update()
{
// 3 if()s for the tap detection (copypasted from documentation)
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(touch.position), Vector2.zero);
// if object is tapped AND tap is below the y = -2.5 coord.
if (Camera.main.ScreenToWorldPoint(touch.position).y <= -2.5f && hit.collider.isTrigger)
{
//shoot back up in random direction
float randx = Random.Range(-5f, 5f);
float randy = Random.Range(6f, 9f);
rb.velocity = new Vector3(randx, randy, 0f);
}
}
}
}
The game works as intended on multiple devices but all share a problem. Sometimes Unity wouldn't register a tap on the object's collider and miss. I recorded and studied it frame by frame and it indeed sometimes doesn't read the tap even inside the collider area. I'm not even sure if it's physics, collision, trigger, tap-detection, or even frame per frame performance related (even though it runs smooth on old devices too, but modern ones still have the issue). I would like to avoid changing the timestep from 0.02 to 0.01 because that would mean change all Time.time and deltaTime in the code, but do tell me if it would help.
Another thread suggested to make the collider bigger/offset upwards to balance out a possible lag in the raycast-collider detection. Is this it?
Have you tried setting the fixed timestep lower? The physics framerate is independent of the game's framerate so no changes need to be made to deltaTime.
I did consider it but I don't know anymore what that would imply. Some sources say it would modify physics but you say it wouldn't, and this is without considering animations. I DID though fix the issue, or rather, ditched the raycast completely. I discovered that the On$$anonymous$$ouseEnter() function works with screentaps too and is so much more reliable and responsive for fast paced registers. And editing in unity is easier since I'm using the same input system. I don't know if it's wrong or not, but it works like a charm.
Some sources say it would modify physics but you say it wouldn't
It does. Fixed timestep shows the time in seconds between each physics frame. (a little weird when most engines set a 'physics fps' value, not a 'seconds' value)
Time.deltaTime is the time elapsed since the last frame which won't affect nor is affected by the physics framerate, Time.fixedDeltaTime is the time elapsed since the last physics frame.
Your answer
Follow this Question
Related Questions
How can I detect a collision point, but allow player to pass through collider. 1 Answer
How to make work a collision when is invisible? 3 Answers
NPCs collide with collider but player does not 1 Answer
How to get collision point when using onTriggerEnter 5 Answers
Objects placed manually collide, but don't if instantiated? 1 Answer