void Update() { //クリックされたら if (Input.GetMouseButtonDown(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // オブジェクトを取得 RaycastHit2D hit2d = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction); //何かしら取得でき、それが自分自身だったら if(hit2d && hit2d.transform.gameObject == this.gameObject) { // 処理を実装 } } }
void OnTriggerStay2D(Collider2D col) { // クリック時イベント if (Input.GetMouseButtonUp(0)) { // このゲームオブジェクトがタッチされたらRayを発射 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit2D hit2d = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction); if (hit2d && hit2d.collider.gameObject == this.gameObject) { // Debug.Log("検知!"); // SetActiveをOFFに this.gameObject.SetActive(false); } } }