Created
November 21, 2015 02:40
-
-
Save OutlawGameTools/5947d54e3effecb53fcc to your computer and use it in GitHub Desktop.
Drag a 2D game object using the mouse. Game Object must have a Collider2D component.
This file contains 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
using UnityEngine; | |
using System.Collections; | |
public class DragWithMouse : MonoBehaviour { | |
private Vector3 offset; | |
void OnMouseDown () { | |
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0); | |
offset = transform.position - Camera.main.ScreenToWorldPoint(mousePos); | |
} | |
void OnMouseDrag () { | |
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0); | |
transform.position = Camera.main.ScreenToWorldPoint(mousePos) + offset; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment