Created
October 3, 2016 15:26
-
-
Save Giacom/5a120478551003b73166675050a2a5b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Public domain - Alexander Taylor | |
| using UnityEngine; | |
| using System.Collections; | |
| public class Example : MonoBehaviour { | |
| public Transform player; | |
| public float speed; | |
| bool isDragging = false; | |
| Vector2 offset; | |
| void Update() { | |
| // Intially touched the screen | |
| if (Input.GetMouseButtonDown(0)) { | |
| offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - player.position; | |
| isDragging = true; | |
| // Released the touch screen | |
| } else if (Input.GetMouseButtonUp(0)) { | |
| offset = Vector2.zero; | |
| isDragging = false; | |
| } | |
| if (isDragging) { | |
| Vector2 destination = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - offset; | |
| player.position = Vector2.MoveTowards(player.position, destination, speed * Time.deltaTime); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment