Hi,
I realize there’s a lot of questions for this topic already but none of them really fixed this for me.
Maybe I’m just doing it completely wrong, I am very much a noob with unity.
Anyway, I have a 2D sprite on which I have added an Edge collider 2D. I’m trying to detect when I touch it, on my touch device.
I tried to follow another post I found here, but it never registers a touch.
Here’s the code I have so far.
private Collider2D collider;
// Use this for initialization
void Start()
{
collider = GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
CheckForTouch();
}
bool CheckForTouch()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
var wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
var touchPosition = new Vector2(wp.x, wp.y);
if (collider == Physics2D.OverlapPoint(touchPosition)){
Debug.Log("HIT!");
}
else{
Debug.Log("MISS");
}
}
return false;
}