Creature.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class Creature : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IDragHandler {
  6. Weapon weapon;
  7. int health;
  8. private bool isDragged = false;
  9. private CanvasGroup canvasGroup;
  10. private RectTransform rectTransform;
  11. [SerializeField] GameObject creatureSprite;
  12. private void Start() {
  13. canvasGroup = GetComponent<CanvasGroup>();
  14. rectTransform = GetComponent<RectTransform>();
  15. }
  16. public void OnEndDrag(PointerEventData eventData) {
  17. canvasGroup.alpha = 1f;
  18. canvasGroup.blocksRaycasts = true;
  19. }
  20. public void OnBeginDrag(PointerEventData eventData) {
  21. canvasGroup.alpha = 0.6f;
  22. canvasGroup.blocksRaycasts = false;
  23. }
  24. public void OnDrag(PointerEventData eventData) {
  25. //rectTransform.anchoredPosition += eventData.delta / canvas.scaleFactor;
  26. rectTransform.position = Input.mousePosition;
  27. RaycastHit2D hit = Physics2D.Raycast(rectTransform.position, Vector2.down);
  28. if (hit.collider != null) {
  29. Debug.Log("Raycast hit " + hit.collider.name);
  30. if (hit.collider.tag.Equals("Tile")) {
  31. rectTransform.sizeDelta = new Vector2(hit.collider.gameObject.GetComponent<RectTransform>().rect.width, hit.collider.gameObject.GetComponent<RectTransform>().rect.height);
  32. }
  33. }
  34. }
  35. }