Skip to content

Instantly share code, notes, and snippets.

@Giacom
Created October 3, 2016 15:26
Show Gist options
  • Select an option

  • Save Giacom/5a120478551003b73166675050a2a5b9 to your computer and use it in GitHub Desktop.

Select an option

Save Giacom/5a120478551003b73166675050a2a5b9 to your computer and use it in GitHub Desktop.
// 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