EgonMilanVotrubec

MousePointer

Nov 19th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class MousePointer : MonoBehaviour
  4. {
  5.     private Vector2 mouseDownStart;
  6.     private Vector3 objectStart;
  7.     private Vector2 mouseDelta;
  8.  
  9.     private bool dragging = false;
  10.     public float pixelWorldRatio;
  11.  
  12.     void Update ( )
  13.     {
  14.         if ( Input.GetMouseButtonDown ( 0 ) )
  15.         {
  16.             mouseDownStart = Input.mousePosition;
  17.             mouseDelta = Vector2.zero;
  18.             objectStart = transform.localPosition;
  19.             dragging = true;
  20.         }
  21.  
  22.         if ( Input.GetMouseButtonUp ( 0 ) )
  23.         {
  24.             mouseDelta = Vector2.zero;
  25.             dragging = false;
  26.         }
  27.  
  28.         if ( dragging )
  29.         {
  30.             mouseDelta = ( Vector2 ) Input.mousePosition - mouseDownStart;
  31.             gameObject.transform.localPosition = new Vector3 ( objectStart.x + ( mouseDelta.x * pixelWorldRatio ), objectStart.y, objectStart.z + ( mouseDelta.y * pixelWorldRatio ) );
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment