Skip to content

Instantly share code, notes, and snippets.

@chabiribon
Created January 30, 2015 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chabiribon/09c7315f6d8da8c25ff6 to your computer and use it in GitHub Desktop.
Save chabiribon/09c7315f6d8da8c25ff6 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class Drag : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDown()
{
this.screenPoint = Camera.main.WorldToScreenPoint (transform.position);
this.offset = transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Vector3 Point = new Vector3 (Input.mousePosition.x, Input.mousePosition.y,screenPoint.z);
Vector3 tPosition = Camera.main.ScreenToWorldPoint (Point) + this.offset;
transform.position = tPosition;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment