2016-10-04 2 views
0

모바일 장치 용 터치로 게임 개체를 이동하고 싶습니다. 해당 플레이어가 화면의 아무 곳이나 터치하고 손가락을 움직일 수 있고 게임 개체가 움직이지 않고 움직일 수 있습니다. 터치 위치. Unity3D에서 손가락으로 움직이는 방법

여기에 지금까지 내 스크립트 나 스크립트가 거의 작동하고

void Update() { 

    if (Input.touchCount > 0) 
    { 
     Touch _touch = Input.GetTouch(0); // screen has been touched, store the touch 

     if(_touch.phase == TouchPhase.Moved) // finger moved 
     { 
      //offset = Camera.main.ScreenToWorldPoint(new Vector3(_touch.position.x, _touch.position.y, theplayer.transform.position.z)) - theplayer.transform.position; 

      touchPos = Camera.main.ScreenToWorldPoint(new Vector3(_touch.position.x, _touch.position.y, theplayer.transform.position.z)); 

      theplayer.transform.position = Vector2.Lerp(theplayer.transform.position, touchPos, Time.deltaTime*5f); 

     } 
     else if(_touch.phase == TouchPhase.Ended){ 
      touchPos = Vector3.zero; 
      offset = Vector3.zero; 
     } 

    } 

} // end 

을했지만 나는 그래서이 게임 오브젝트를 볼 수 없습니다 손가락 아래의 화면에서 게임 오브젝트의 움직임을 터치하면 문제이다. 나는 이걸 내가 화면의 아무 곳이나 터치하고 손가락으로 움직이지 않고 손가락으로 움직이기를 원한다.

감사합니다.

답변

1

여기 해결책 코드는 스스로 해결했습니다.

// Update is called once per frame 
    void Update() { 

     if (Input.touchCount > 0) 
     { 
      _touch = Input.GetTouch(0); // screen has been touched, store the touch 

      if(_touch.phase == TouchPhase.Began){ 
       isDragging = true; 

       offset = Camera.main.ScreenToWorldPoint(new Vector2(_touch.position.x, _touch.position.y)) - theplayer.transform.position; 

      } 
      else if(_touch.phase == TouchPhase.Ended){ 
       offset = Vector2.zero; 
       isDragging = false; 
      } 

     } 

     if(isDragging){ 
      Vector2 _dir = Camera.main.ScreenToWorldPoint(new Vector2(_touch.position.x, _touch.position.y)); 
      _dir = _dir - offset; 

      theplayer.transform.position = Vector2.Lerp(theplayer.transform.position, _dir, Time.deltaTime * speed); 

     } 


    } // end 
+0

이것은 완전한 스크립트가 아니므로 속도, isDragging (bool 유형)과 같은 변수를 선언해야합니다. –

관련 문제