Advertisement
Atomic_Violetta

Untitled

Jul 30th, 2024
93
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.15 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Drag_Drop : MonoBehaviour
  6. {
  7.     private bool isDragging = false;
  8.     private bool isOverplayerdropZone = false;
  9.     private GameObject playerdropZone;
  10.     private Vector2 startPosition;
  11.  
  12.  
  13.     // Update is called once per frame
  14.     void Update()
  15.     {
  16.         if (isDragging)
  17.         {
  18.             transform.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
  19.         }
  20.     }
  21.    
  22.     private void OnCollisionEnter2D(Collision2D collision)
  23.     {
  24.         playerdropZone = collision.gameObject;
  25.     }
  26.  
  27.     private void OnCollisionExit2D(Collision2D collision)
  28.     {
  29.         isOverplayerdropZone = false;
  30.         playerdropZone = null;
  31.     }
  32.     public void StartDrag()
  33.     {
  34.         startPosition = transform.position;
  35.         isDragging = true;
  36.     }
  37.  
  38.     public void EndDrag()
  39.     {
  40.         isDragging = false;
  41.         if (isOverplayerdropZone)
  42.         {
  43.             transform.SetParent(playerdropZone.transform, false);
  44.         }
  45.         else
  46.         {
  47.             transform.position = startPosition;
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Comments
  • IAFahim
    317 days (edited)
    # C# 1.72 KB | 0 0
    1. using UnityEngine;
    2.  
    3. public class Drag_Drop : MonoBehaviour
    4. {
    5.     public bool isDragging = false;
    6.     public Vector2 offset;
    7.     public Vector2 startPosition;
    8.     public Camera cam;
    9.    
    10.     public GameObject playerdropZone;
    11.     public bool isOverplayerdropZone = false;
    12.     public bool dropInOther = true;
    13.  
    14.  
    15.     private void Start()
    16.     {
    17.         startPosition = transform.position;
    18.     }
    19.  
    20.     private void OnMouseDown()
    21.     {
    22.         isDragging = true;
    23.         offset = GetMousePos() - (Vector2)transform.position;
    24.     }
    25.  
    26.     private void OnMouseDrag()
    27.     {
    28.         Vector2 targetPos = GetMousePos() - offset;
    29.         transform.position = targetPos;
    30.     }
    31.  
    32.     private void OnMouseUp()
    33.     {
    34.         isDragging = false;
    35.     }
    36.  
    37.     private void OnTriggerEnter2D(Collider2D other)
    38.     {
    39.         playerdropZone = other.gameObject;
    40.         isOverplayerdropZone = true;
    41.     }
    42.  
    43.     private void OnTriggerExit2D(Collider2D other)
    44.     {
    45.         isOverplayerdropZone = false;
    46.         playerdropZone = null;
    47.     }
    48.    
    49.  
    50.     private void Update()
    51.     {
    52.         if (isDragging)
    53.         {
    54.             if (isOverplayerdropZone)
    55.             {
    56.                 transform.SetParent(playerdropZone.transform,false);
    57.                 transform.localPosition = Vector3.zero;
    58.             }    
    59.         }
    60.         else
    61.         {
    62.             if (!dropInOther)
    63.             {
    64.                 transform.SetParent(null);
    65.                 transform.position = startPosition;    
    66.             }
    67.         }
    68.     }
    69.  
    70.     private Vector2 GetMousePos()
    71.     {
    72.         var flippedCamZ = -cam.transform.position.z;
    73.         return cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, flippedCamZ));
    74.     }
    75. }
Add Comment
Please, Sign In to add comment
Advertisement