카테고리 없음

[Unity] 클릭 혹은 터치한 곳의 오브젝트 정보 얻어내기

오즈마스터 2019. 9. 28. 10:13

어떤 오브젝트를 터치했는지 알아내서 처리하는 코드

void Update()
{
  if (Input.GetMouseButton(0))
  {
    Vector2 touchPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);            
    RaycastHit2D hitInformation = Physics2D.Raycast(touchPos, Camera.main.transform.forward);
    if (hitInformation.collider != null)
    {
      //We should have hit something with a 2D Physics collider!
      GameObject touchedObject = hitInformation.transform.gameObject;
      //touchedObject should be the object someone touched.
        ...