Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DragObject : MonoBehaviour {
- private Vector3 mOffset;
- private float mZCoord;
- //private Vector3 currentPosition;
- void OnMouseDown()
- {
- mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
- // Store offset = gameObject world pos - mouse world pos
- mOffset = gameObject.transform.position - GetMouseWorldPos();
- // currentPosition = gameObject.transform.position;
- }
- void OnMouseDrag()
- {
- transform.position = GetMouseWorldPos() + mOffset;
- }
- /*private void OnMouseUp()
- {
- transform.position = currentPosition;
- }*/
- private Vector3 GetMouseWorldPos()
- {
- //pixel coordinates (x,y)
- Vector3 mousePoint = Input.mousePosition;
- //z coordinate of game object on screen
- mousePoint.z = mZCoord;
- return Camera.main.ScreenToWorldPoint(mousePoint);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement