TouchMovement
How do I use Touch Movement
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
Line | Code | Ex.. |
---|---|---|
37 | TouchMovement (); | 1 |
55 | void TouchMovement(){ | 2 |
File name: PlayerController.cs
Copy
31 void Update () {
32 if (GameplayController.instance.gameInProgress) {
33 LimitPosition ();
34 if (Application.platform == RuntimePlatform.WindowsEditor) {
35 PlayerMovement ();
36 }else if(Application.platform == RuntimePlatform.Android){
37 TouchMovement ();
38 }
39
40 }
41 }
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 }
TouchMovement 115 lượt xem
Gõ tìm kiếm nhanh...