2016-08-13 2 views
0

유니티에 익숙하지 않고 모바일 2d 게임을 개발했습니다. 이제 스크린 센터 전후의 스크린을 터치 할 때 오브젝트를 좌우로 움직일 수있게되었습니다. 하지만 손가락을 화면에 대고 움직이는 동안 객체를 만져서 x 축으로 드래그하여 객체가 내 손가락의 같은 x 위치에 있도록하고 싶습니다. 누구나 할 수 있습니다. 그것을 올바르게 : 여기에 내가 화면 중앙 이전 또는 이후에 만지면 내가 객체를 이동하고 방법의 코드는 다음과 같습니다유니티 게임 손가락 움직임으로 오브젝트 끌기

public class paddle : MonoBehaviour { 

    public Rigidbody2D rb; 
    public float speed; 
    public float maxX; 
    bool currentisAndroid=false; 
    // Use this for initialization 
    void Start() { 
     rb = GetComponent<Rigidbody2D>(); 
     #if UNITY_ANDROID 
     currentisAndroid=true; 
     #else 
     currentisAndroid=false; 
     #endif 
    } 

    // Update is called once per frame 
    void Update() { 
     if (currentisAndroid == true) { 
      if (Input.GetTouch (0).position.x < Screen.width/2 && Input.GetTouch (0).phase == TouchPhase.Stationary) 
       moveLeft(); 
      else if (Input.GetTouch (0).position.x > Screen.width/2 && Input.GetTouch (0).phase == TouchPhase.Stationary) 
       moveRight(); 
      else 
       stop(); 

     } else { 
      float x = Input.GetAxis ("Horizontal"); 
      //if (Input.GetTouch (0).position.x == rb.position.x && Input.GetTouch (0).phase == TouchPhase.Moved) 
      if (x == 0) 
       stop(); 
      if (x < 0) 
       moveLeft(); 
      if (x > 0) 
       moveRight(); 

      Vector2 pos = transform.position; 
      pos.x=Mathf.Clamp (pos.x,-maxX,maxX); 
      transform.position = pos; 
     } 


    } 
    void moveLeft() 
    { 
     rb.velocity = new Vector2 (-speed, 0); 
    } 
    void moveRight() 
    { 
     rb.velocity = new Vector2 (speed, 0); 
    } 
    void stop() 
    { 
     rb.velocity = new Vector2 (0, 0); 
    } 
    public float getposition() 
    { 
     return rb.position.y; 
    } 
} 

답변

0

가장 쉬운 방법 :

구성 요소를 DragRigidbody 스크립트를 추가하고 마우스 나 터치 스크린 접촉을 통해 개체를 끌어 할 수있을 것입니다.

0

만약 내가 제대로 이해 :
1 - 레이 캐스트를 손가락 위치에서 수직으로 카메라와 함께 장면.
2 - 적중 개체를 선택하십시오.
3 - 카메라를 세계 좌표에 매핑하고지도 또는 게임 개체 \ 개체를 사용하여 광선의 도달 지점에 따라 해당 개체를 이동합니다.

  • Physics.Raycast();
  • RaycastHit.collider();
  • Camera.main.ScreenToWorldPoint (Input.GetTouch (< 0 또는 1 또는 모두>) 위치);

지도를 가로 질러 물체를 움직이려면 터치를 추적 할 수 있고 모퉁이가 가까워지면 그 직선 (수평 - 수직)으로 카메라를 움직일 수 있습니다.

관련 문제