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 DragDrop : MonoBehaviour
- {
- private Vector3 mOffset;
- public Vector3 cardPos; //Center of Play Area
- public Vector3 cardPosStatic; //Original position which should only change when a card is drawn
- private Vector3 cardPosLast;
- public bool playAllowed;
- private Vector3 setCardDepth;
- private float mZCoord;
- public bool castChecker;
- private bool firstClick;
- void Start()
- {
- setCardDepth = new Vector3(0,0,-5);
- cardPos = new Vector3(0,300,-5);
- cardPosLast = gameObject.transform.position;
- }
- private void OnCollisionEnter(Collision other)
- {
- if (other.gameObject.tag == "castArea")
- {
- playAllowed = true;
- }
- }
- private void OnCollisionExit (Collision other)
- {
- playAllowed = false;
- }
- public void ResetPos()
- {
- gameObject.transform.position = cardPosStatic;
- playAllowed = false;
- castChecker = false;
- }
- void OnMouseDown()
- {
- if (!firstClick)
- {
- cardPosStatic = gameObject.transform.position;
- firstClick = true;
- }
- cardPosLast = gameObject.transform.position;
- mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
- // Store offset = gameobject world pos - mouse world pos
- mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
- }
- private Vector3 GetMouseAsWorldPoint()
- {
- // Pixel coordinates of mouse (x,y)
- Vector3 mousePoint = Input.mousePosition;
- // z coordinate of game object on screen
- mousePoint.z = mZCoord;
- // Convert it to world points
- return Camera.main.ScreenToWorldPoint(mousePoint);
- }
- void OnMouseDrag()
- {
- transform.position = (GetMouseAsWorldPoint() + mOffset) + setCardDepth;
- }
- void OnMouseUp()
- {
- if (playAllowed)
- {
- gameObject.transform.position = cardPos;
- castChecker = true;
- }
- if (playAllowed == false)
- {
- ResetPos();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement