GetTouch









How do I use Get Touch
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: InputToEvent.cs Copy
32     void Update()
33     {
34         if( DetectPointedAtGameObject )
35         {
36             goPointedAt = RaycastObject( Input.mousePosition );
37         }
38
39         if( Input.touchCount > 0 )
40         {
41             Touch touch = Input.GetTouch( 0 );
42             this.currentPos = touch.position;
43
44             if( touch.phase == TouchPhase.Began )
45             {
46                 Press( touch.position );
47             }
48             else if( touch.phase == TouchPhase.Ended )
49             {
50                 Release( touch.position );
51             }
52
53             return;
54         }
55
56         currentPos = Input.mousePosition;
57         if( Input.GetMouseButtonDown( 0 ) )
58         {
59             Press( Input.mousePosition );
60         }
61         if( Input.GetMouseButtonUp( 0 ) )
62         {
63             Release( Input.mousePosition );
64         }
65
66         if( Input.GetMouseButtonDown( 1 ) )
67         {
68             pressedPosition = Input.mousePosition;
69             lastGo = RaycastObject( pressedPosition );
70             if( lastGo != null )
71             {
72                 lastGo.SendMessage( "OnPressRight", SendMessageOptions.DontRequireReceiver );
73             }
74         }
75     }
File name: PlayerController.cs Copy
55  void TouchMovement(){
56   if(Input.touchCount > 0){
57    Touch touch = Input.GetTouch (0);
58
59    if(touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Began){
60     Vector3 fingerPos = touch.position;
61     position.x = fingerPos.x;
62     realPos = Camera.main.ScreenToWorldPoint (new Vector3(position.x, position.y ,position.z));
63     transform.position = Vector3.Lerp (transform.position, new Vector3(Mathf.Clamp(realPos.x, maxLeft + 0.5f, maxRight - 0.5f), transform.position.y, transform.position.z), Time.deltaTime * speed);
64
65    }
66
67    if (touch.phase == TouchPhase.Moved) {
68     Vector3 fingerPos = touch.position;
69     position.x = fingerPos.x;
70     realPos = Camera.main.ScreenToWorldPoint (new Vector3(position.x, position.y ,position.z));
71     transform.position = Vector3.Lerp (transform.position, new Vector3(Mathf.Clamp(realPos.x, maxLeft + 0.5f, maxRight - 0.5f), transform.position.y, transform.position.z), speed);
72    }
73
74   }
75  }
File name: CameraFollow.cs Copy
91  void TouchMoveCamera(){
92   if(allowToMove){
93
94    /*if(Input.touchCount > 0){
95     Touch touch = Input.GetTouch (0);
96
97     Transform cam = gameObject.transform;
98
99
100     if(touch.phase == TouchPhase.Began){
101      cam = touch.position;
102     }else if(touch.phase == TouchPhase.Moved){
103
104     }
105    }*/
106
107    if(Input.touchCount > 0){
108     Touch touch = Input.GetTouch (0);
109
110     Transform cam = gameObject.transform;
111     if (touch.position.x > 300) {
112      if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Stationary){
113       camPos = touch.position;
114      }else if(touch.phase == TouchPhase.Moved){
115       cam.position = cam.position + (new Vector3 ((camPos.x - touch.position.x), 0, 0) * dragSpeed * Time.deltaTime);
116       float camX = cam.position.x;
117       camX = Mathf.Clamp (camX, leftBound.transform.position.x, rightBound.transform.position.x);
118       cam.position = new Vector3 (camX, cam.position.y, cam.position.z);
119      }
120     }
121
122    }
123
124   }
125  }
File name: Cannon.cs Copy
63  void TouchCannonShoot(){
64   if(Input.touchCount > 0){
65    Touch touch = Input.GetTouch (0);
66
67    touchPos = Camera.main.ScreenToWorldPoint (touch.position);
68
69    Vector2 touchRayHit = new Vector2 (touchPos.x, touchPos.y);
70
71    RaycastHit2D hit = Physics2D.Raycast (touchRayHit , Vector2.zero);
72
73    if(hit.collider != null){
74     if(hit.collider.CompareTag("Player")){
75      if(touch.phase == TouchPhase.Stationary){
76       if (shot != 0) {
77        UpdatePowerLevel ();
78        isCharging = true;
79       }
80      }else if(touch.phase == TouchPhase.Ended){
81       if (shot != 0) {
82        GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
83        newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
84        if (GameController.instance != null && MusicController.instance != null) {
85         if (GameController.instance.isMusicOn) {
86          audioSource.PlayOneShot (cannonShot);
87         }
88        }
89        shot--;
90        powerLevel.value = 0;
91        readyToShoot = false;
92        isCharging = false;
93       }
94      }
95     }
96    }
97
98    /*if(touch.phase == TouchPhase.Stationary){
99     if (shot != 0) {
100      UpdatePowerLevel ();
101     }
102    }else if(touch.phase == TouchPhase.Ended){
103     if (shot != 0) {
104      GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
105      newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
106      if (GameController.instance != null && MusicController.instance != null) {
107       if (GameController.instance.isMusicOn) {
108        audioSource.PlayOneShot (cannonShot);
109       }
110      }
111      shot--;
112      powerLevel.value = 0;
113      readyToShoot = false;
114     }
115    }*/
116
117   }
118  }
File name: Cannon.cs Copy
172  void TouchCannonMovement(){
173   if(Input.touchCount > 0){
174    Touch touch = Input.GetTouch (0);
175
176    if (touch.position.x < 300 && !isCharging && touch.position.y > 160) {
177
178     if (touch.phase == TouchPhase.Moved) {
179      Vector2 touchDeltaPosition = touch.deltaPosition;
180      direction = touchDeltaPosition.normalized;
181
182      if (direction.y > 0 && touchDeltaPosition.x > -10 && touchDeltaPosition.x < 10) {
183       currentRotation.z += 50f * Time.deltaTime;
184      } else if (direction.y < 0 && touchDeltaPosition.x > -10 && touchDeltaPosition.x < 10) {
185       currentRotation.z -= 50f * Time.deltaTime;
186      }
187
188     }
189    }
190   }
191
192   currentRotation.z = Mathf.Clamp(currentRotation.z, minRot, maxRot);
193
194   transform.rotation = Quaternion.Euler (currentRotation);
195
196  }

GetTouch 123 lượt xem

Gõ tìm kiếm nhanh...