Advertisement
Guest User

DragDrop

a guest
Feb 15th, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class DragDrop : MonoBehaviour
  6. {
  7.     private Vector3 mOffset;
  8.     public Vector3 cardPos; //Center of Play Area
  9.     public Vector3 cardPosStatic; //Original position which should only change when a card is drawn
  10.     private Vector3 cardPosLast;
  11.     public bool playAllowed;
  12.     private Vector3 setCardDepth;
  13.     private float mZCoord;
  14.     public bool castChecker;
  15.     private bool firstClick;
  16.  
  17.     void Start()
  18.     {
  19.         setCardDepth = new Vector3(0,0,-5);
  20.         cardPos = new Vector3(0,300,-5);
  21.         cardPosLast = gameObject.transform.position;
  22.     }
  23.  
  24.     private void OnCollisionEnter(Collision other)
  25.     {
  26.         if (other.gameObject.tag == "castArea")
  27.         {
  28.             playAllowed = true;
  29.         }
  30.     }
  31.  
  32.     private void OnCollisionExit (Collision other)
  33.     {
  34.         playAllowed = false;
  35.     }
  36.  
  37.     public void ResetPos()
  38.     {
  39.         gameObject.transform.position = cardPosStatic;
  40.         playAllowed = false;
  41.         castChecker = false;
  42.     }
  43.  
  44.     void OnMouseDown()
  45.     {
  46.         if (!firstClick)
  47.         {
  48.             cardPosStatic = gameObject.transform.position;
  49.             firstClick = true;
  50.         }
  51.         cardPosLast = gameObject.transform.position;
  52.         mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
  53.         // Store offset = gameobject world pos - mouse world pos
  54.         mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
  55.     }
  56.  
  57.     private Vector3 GetMouseAsWorldPoint()
  58.     {
  59.         // Pixel coordinates of mouse (x,y)
  60.         Vector3 mousePoint = Input.mousePosition;
  61.         // z coordinate of game object on screen
  62.         mousePoint.z = mZCoord;
  63.         // Convert it to world points
  64.         return Camera.main.ScreenToWorldPoint(mousePoint);
  65.     }
  66.  
  67.     void OnMouseDrag()
  68.     {
  69.             transform.position = (GetMouseAsWorldPoint() + mOffset) + setCardDepth;
  70.     }
  71.  
  72.     void OnMouseUp()
  73.     {
  74.         if (playAllowed)
  75.         {
  76.             gameObject.transform.position = cardPos;
  77.             castChecker = true;
  78.  
  79.         }
  80.         if (playAllowed == false)
  81.         {
  82.             ResetPos();
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement