ambrose01

GrapplePoint.cs

Jan 1st, 2026
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class GrapplePoint : MonoBehaviour
  5. {
  6. [Header("Settings")]
  7. public float grappleRange = 15f;
  8. public float minGrappleRange = 4f;
  9.  
  10. [Header("UI Adjustment")]
  11. [Range(0.1f, 2f)]
  12. public float uiScaleMultiplier = 0.5f;
  13. public GameObject uiPrefab;
  14.  
  15. [HideInInspector] public bool isTargeted = false;
  16.  
  17. private Transform player;
  18. private GameObject myUI;
  19. private Image uiImage;
  20. private RectTransform uiRect;
  21. private Camera cam;
  22. private Canvas mainCanvas;
  23.  
  24. void Start()
  25. {
  26. player = GameObject.FindGameObjectWithTag("Player").transform;
  27. cam = Camera.main;
  28. mainCanvas = FindObjectOfType<Canvas>();
  29.  
  30. if (uiPrefab != null && mainCanvas != null)
  31. {
  32. myUI = Instantiate(uiPrefab, mainCanvas.transform);
  33. uiImage = myUI.GetComponent<Image>();
  34. uiRect = myUI.GetComponent<RectTransform>();
  35. myUI.transform.SetAsFirstSibling();
  36. }
  37. }
  38.  
  39. void Update()
  40. {
  41. if (myUI == null || player == null) return;
  42.  
  43. float dist = Vector3.Distance(transform.position, player.position);
  44.  
  45. if (dist < minGrappleRange)
  46. {
  47. myUI.SetActive(false);
  48. return;
  49. }
  50.  
  51. Vector3 screenPos = cam.WorldToScreenPoint(transform.position);
  52. if (screenPos.z < 0)
  53. {
  54. myUI.SetActive(false);
  55. return;
  56. }
  57.  
  58. myUI.SetActive(true);
  59. uiRect.position = screenPos;
  60.  
  61. if (dist <= grappleRange)
  62. {
  63. uiImage.color = Color.green;
  64.  
  65. float finalScale = isTargeted ? (uiScaleMultiplier * 1.2f) : uiScaleMultiplier;
  66. myUI.transform.localScale = Vector3.one * finalScale;
  67. }
  68. else
  69. {
  70. uiImage.color = Color.white;
  71. myUI.transform.localScale = Vector3.one * (uiScaleMultiplier * 0.8f);
  72. }
  73.  
  74. isTargeted = false;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment