Hello !
I use this codes to detect touch on 2d gameobjects . but it doesn’t work on moving 2d gameobjects . Pleas help me to find the right code . thanks
public GameObject a;
public Transform ag;
float xsp;
float ysp;
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch (0);
if (touch.phase == TouchPhase.Ended) {
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint ((touch.position)), Vector2.zero);
if (hit.collider != null && hit.collider.transform.gameObject.name == "a") {
totalscore += 10;
a.SetActive (false);
}
}
}
My gameobject moves by this code :
void act3 () {
xsp -= 0.5f*Time.deltaTime*speed1;
ysp += 0.2f*Time.deltaTime*speed2;
ag = new Vector2 (-4.2f+xsp , -3.1f+ysp);
a.gameObject.transform.position = ag;
}