dmitryzenevich

DragPhysics2DObjectVer2

Oct 23rd, 2021
734
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3.  
  4. namespace Levels.Logic
  5. {
  6.     public class DragPhysics2DObjectVer2 : MonoBehaviour, IDragHandler, IEndDragHandler
  7.     {
  8.         [SerializeField] private Camera _camera;
  9.         [SerializeField] private Rigidbody2D _rigidbody2D = null;
  10.         [SerializeField] private float _velocityMultiplier = 10f;
  11.         [SerializeField] private bool _otherRigidbody;
  12.         [SerializeField] private MoveType _moveType = MoveType.XY;
  13.         [SerializeField] private bool _isFreezePositionWhenNotMoved = false;
  14.  
  15.         private Vector3 _previousDragPosition;
  16.         private Vector2 _direction;
  17.         private Vector3 _currentDragPosition;
  18.         private Vector2 _newPosition;
  19.         private bool _isCanDrag = true;
  20.         private bool _isCanMove = false;
  21.  
  22.         private Vector2 _offset;
  23.         private bool _isInput = false;
  24.         private bool _isClickOnObject = false;
  25.         private RigidbodyConstraints2D _previousConstraint;
  26.         public bool IsClickOnObject => _isClickOnObject;
  27.  
  28.         public bool IsCanDrag
  29.         {
  30.             get => _isCanDrag;
  31.             set => _isCanDrag = value;
  32.         }
  33.  
  34.         public Rigidbody2D Rigidbody2D => _otherRigidbody
  35.             ? _rigidbody2D
  36.             : _rigidbody2D != null && ReferenceEquals(_rigidbody2D.gameObject, gameObject)
  37.                 ? _rigidbody2D
  38.                 : _rigidbody2D = GetComponent<Rigidbody2D>();
  39.  
  40.         public Camera Camera => _camera != null ? _camera : _camera = Camera.main;
  41.  
  42. #if UNITY_EDITOR
  43.         private void OnValidate()
  44.         {
  45.             var rigidbody2D1 = Rigidbody2D;
  46.             var camera1 = Camera;
  47.         }
  48. #endif
  49.  
  50.         private void Awake()
  51.         {
  52.             var camera1 = Camera;
  53.             IsCanDrag = true;
  54.             _previousConstraint = Rigidbody2D.constraints;
  55.         }
  56.  
  57.         public void OnDrag(PointerEventData eventData)
  58.         {
  59.             if (IsCanDrag == false)
  60.             {
  61.                 Rigidbody2D.velocity = Vector2.zero;
  62.                 return;
  63.             }
  64.  
  65.             var screenToWorldPoint = (Vector2) Camera.ScreenToWorldPoint(eventData.position);
  66.             Rigidbody2D.velocity = (screenToWorldPoint - (Rigidbody2D.position + _offset)) * _velocityMultiplier;
  67.         }
  68.  
  69.  
  70.         public void OnEndDrag(PointerEventData eventData)
  71.         {
  72.             Rigidbody2D.velocity = Vector2.zero;
  73.         }
  74.  
  75.         private enum MoveType
  76.         {
  77.             XY,
  78.             X,
  79.             Y,
  80.         }
  81.     }
  82. }
RAW Paste Data