Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- public class GrapplePoint : MonoBehaviour
- {
- [Header("Settings")]
- public float grappleRange = 15f;
- public float minGrappleRange = 4f;
- [Header("UI Adjustment")]
- [Range(0.1f, 2f)]
- public float uiScaleMultiplier = 0.5f;
- public GameObject uiPrefab;
- [HideInInspector] public bool isTargeted = false;
- private Transform player;
- private GameObject myUI;
- private Image uiImage;
- private RectTransform uiRect;
- private Camera cam;
- private Canvas mainCanvas;
- void Start()
- {
- player = GameObject.FindGameObjectWithTag("Player").transform;
- cam = Camera.main;
- mainCanvas = FindObjectOfType<Canvas>();
- if (uiPrefab != null && mainCanvas != null)
- {
- myUI = Instantiate(uiPrefab, mainCanvas.transform);
- uiImage = myUI.GetComponent<Image>();
- uiRect = myUI.GetComponent<RectTransform>();
- myUI.transform.SetAsFirstSibling();
- }
- }
- void Update()
- {
- if (myUI == null || player == null) return;
- float dist = Vector3.Distance(transform.position, player.position);
- if (dist < minGrappleRange)
- {
- myUI.SetActive(false);
- return;
- }
- Vector3 screenPos = cam.WorldToScreenPoint(transform.position);
- if (screenPos.z < 0)
- {
- myUI.SetActive(false);
- return;
- }
- myUI.SetActive(true);
- uiRect.position = screenPos;
- if (dist <= grappleRange)
- {
- uiImage.color = Color.green;
- float finalScale = isTargeted ? (uiScaleMultiplier * 1.2f) : uiScaleMultiplier;
- myUI.transform.localScale = Vector3.one * finalScale;
- }
- else
- {
- uiImage.color = Color.white;
- myUI.transform.localScale = Vector3.one * (uiScaleMultiplier * 0.8f);
- }
- isTargeted = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment