Hello,
The title might be overstating it, but we are developing an action game and it requires fast and precise touches. However, sometimes, mainly during fps spikes (spikes = 25-30 fps, down from the regular 60 fps), the touch is… unresponsive, some of the touches are not interpreted.
My code for touch input detection is as follows:
public class TouchHandlerGame : MonoBehaviour
{
//public TextMesh debugText;
private bool buttonClick;
private Camera mainCamera;
private bool mobile;
//Variables for mouse slash
private Vector3 initialSlashPos;
private Vector2 initialTouchPos;
private Vector2 initialClickCancelPos;
private void Start()
{
mainCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
mobile = true;
}
private void Update()
{
if (mobile)
{
foreach (Touch touch in Input.touches)
{
if (touch.fingerId == 0)
{
if (touch.phase == TouchPhase.Began)
{
buttonClick = true;
}
if (touch.phase == TouchPhase.Moved)
{
//buttonClick = false;
Ray ray = mainCamera.ScreenPointToRay(touch.position);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.collider.tag == "Slashable")
{
if (initialTouchPos == Vector2.zero)
initialTouchPos = touch.position;
{
float sqrLen = (initialTouchPos - touch.position).sqrMagnitude;
if (sqrLen > 200)
{
initialTouchPos = Vector2.zero;
hit.collider.gameObject.SendMessage("OnMouseSlash", SendMessageOptions.DontRequireReceiver);
}
}
}
if (initialClickCancelPos == Vector2.zero)
initialClickCancelPos = touch.position;
{
float sqrLen = (initialClickCancelPos - touch.position).sqrMagnitude;
if (sqrLen > 200)
{
initialClickCancelPos = Vector2.zero;
buttonClick = false;
}
}
}
}
if (touch.phase == TouchPhase.Ended)
{
if (buttonClick)
{
buttonClick = false;
Ray ray = mainCamera.ScreenPointToRay(touch.position);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.collider.tag != "Button")
hit.collider.gameObject.SendMessage("OnMouseClickDown", SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
}
}
}
The game is playable, it doesnt happen all that often, but more often the desired… am I detecting touches wrong? Does anyone else have this kind of problem?
Thank you for your attention,
Best regards,
Allan