I have a game where there are objects that move across the screen. You tap on them to stop them. I have an input manager script that either gets mouse input or touch input and sends a message to the object the ray hit. This works flawlessly when testing in the editor with a mouse. However, when I export to android on my Galaxy S6, tapping on the upper half of the object doesn’t stop the object. The objects do move relatively quickly across the screen, but I am fairly certain I’m tapping directly on the objects collider. It only seems to recognize taps on the bottom half, and a bit below the object. What may be causing this?
InputManager.cs
using UnityEngine;
using System.Collections;
public class InputManager : MonoBehaviour {
private RaycastHit2D hit;
void Update () {
#if UNITY_EDITOR
if(Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
mousePos.z = 10;
Vector3 screenPos = Camera.main.ScreenToWorldPoint(mousePos);
RaycastHit2D hit = Physics2D.Raycast(screenPos,Vector2.zero);
if(hit)
{
GameObject recipient = hit.transform.gameObject;
recipient.SendMessage("OnTouch", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
#else
if(Input.touchCount > 0)
{
foreach(Touch touch in Input.touches)
{
if(touch.phase == TouchPhase.Began)
{
Vector3 screenPos = Camera.main.ScreenToWorldPoint(touch.position);
RaycastHit2D hit = Physics2D.Raycast(screenPos,Vector2.zero);
if(hit)
{
GameObject recipient = hit.transform.gameObject;
recipient.SendMessage("OnTouch", hit.point, SendMessageOptions.DontRequireReceiver);
}
}
}
}
#endif
}
}