I have a Camera that follows my Player, so I want the user to “Tap” “Coins” to make them fall. I am using Physics2D.Raycast to detect a collision between a circle collider and a touch Position. The script works, but it is very difficult to “Tap” the coins, specially when the player is running because they move faster. Trying to fix this, I made my circle Collider Radius Bigger than my actual Sprite, It worked a bit better, but when my player moves fast, again does not work well, and also I realized that if I tap on the left side of the coin (Outside the Collider) that will hit my collider. What am I Doing Wrong, or Why does the detection is slow?
Im Testing this on my iPad Mini, but when I test on a smaller device like an iPhone, it gets worst.
This is my script Attached to the coins GameObjects:
using UnityEngine;
using System.Collections;
public class Touch : MonoBehaviour {
public RaycastHit2D hit;
private LayerMask coinLayer;
void Awake()
{
layerJitomates = 1 << LayerMask.NameToLayer ("Coins");
}
void Update ()
{
if (Input.touchCount > 0)
for(int x =0;x<Input.touchCount;x++)
{
hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(x).position),Vector2.zero,coinLayer);
if(hit != null && hit.transform != null && hit.collider.tag == "Coin")
{
if(Input.GetTouch(x).phase == TouchPhase.Began)
{
hit.rigidbody.isKinematic=false;
hit.rigidbody.collider2D.GetComponent<CircleCollider2D>().radius = 1.65F;
hit.rigidbody.collider2D.GetComponent<CircleCollider2D>().isTrigger = false;
}
}
}
}
}