Guest User

Untitled

a guest
Jul 3rd, 2024
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using UnityEngine;
  6.  
  7. public class OffScreenMissileIndicator : MonoBehaviour
  8. {
  9.     //list of the missiles
  10.     public List<GameObject> Targets = new List<GameObject>();
  11.     //marker that shows where the missiles are
  12.     public GameObject indicatorPrefab;
  13.  
  14.     //sprite render for the marker
  15.     private SpriteRenderer spriteRenderer;
  16.     private float spriteWidth;
  17.     private float spriteHeight;
  18.  
  19.     //reference to the camera
  20.     private new Camera camera;
  21.  
  22.     //dictionary that stores the marker and the missile
  23.     private Dictionary<GameObject, GameObject> targetIndicators = new Dictionary<GameObject, GameObject>();
  24.  
  25.     private void OnEnable()
  26.     {
  27.         EventSystemRP.OnMissileSpawned += OnMissileSpawned;
  28.         EventSystemRP.OnMissileDestroyed += OnMissileDestroyed;
  29.     }
  30.  
  31.     private void OnDisable()
  32.     {
  33.         EventSystemRP.OnMissileSpawned -= OnMissileSpawned;
  34.         EventSystemRP.OnMissileDestroyed -= OnMissileDestroyed;
  35.     }
  36.  
  37.     private void OnMissileSpawned(GameObject missile)
  38.     {
  39.         Targets.Add(missile);
  40.     }
  41.  
  42.     private void OnMissileDestroyed(GameObject missile)
  43.     {
  44.         Targets.Remove(missile);
  45.     }
  46.  
  47.     // Start is called before the first frame update
  48.     void Start()
  49.     {
  50.         //getting the camera and setting the indicator prefab to the sprite renderer
  51.         camera = Camera.main;
  52.         spriteRenderer = indicatorPrefab.GetComponent<SpriteRenderer>();
  53.  
  54.         //getting the size of the sprite
  55.         var bounds = spriteRenderer.bounds;
  56.         spriteHeight = bounds.size.y/2f;
  57.         spriteWidth = bounds.size.x/2f;
  58.  
  59.         //instantiating the marker
  60.         foreach (var target in Targets)
  61.         {
  62.            var marker = Instantiate(indicatorPrefab);
  63.            marker.SetActive(false);
  64.            targetIndicators.Add(target, marker);
  65.         }
  66.     }
  67.  
  68.     // Update is called once per frame
  69.     void Update()
  70.     {
  71.         //updating the position of the marker
  72.         foreach (KeyValuePair<GameObject, GameObject> entry in targetIndicators)
  73.         {
  74.             var marker = entry.Key;
  75.             var missile = entry.Value;
  76.  
  77.             UpdateMissile(marker, missile);
  78.         }
  79.        
  80.     }
  81.  
  82.     //function that updates the position and rotation of the marker clamping its position to the edges of the screen.
  83.     private void UpdateMissile(GameObject target, GameObject indicator)
  84.     {
  85.         var screenPos = camera.WorldToViewportPoint(target.transform.position);
  86.         bool isOffScreen = screenPos.x <=0 || screenPos.x >=1 || screenPos.y <=0 || screenPos.y >=1;
  87.         if (isOffScreen)
  88.         {
  89.             indicator.SetActive(true);
  90.             var spriteSizeInVeiwPort = camera.WorldToViewportPoint(new Vector3(spriteWidth, spriteHeight, 0))
  91.             - camera.WorldToViewportPoint(Vector3.zero);
  92.  
  93.             screenPos.x = Mathf.Clamp(screenPos.x, spriteSizeInVeiwPort.x, 1 - spriteSizeInVeiwPort.x);
  94.             screenPos.y = Mathf.Clamp(screenPos.y, spriteSizeInVeiwPort.y, 1 - spriteSizeInVeiwPort.y);
  95.  
  96.             var worldPosition = camera.ViewportToWorldPoint(screenPos);
  97.             worldPosition.z = 0;
  98.             indicator.transform.position = worldPosition;
  99.  
  100.             Vector3 direction = target.transform.position - indicator.transform.position;
  101.             float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg + 90f;
  102.             indicator.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
  103.         }
  104.         else
  105.         {
  106.             indicator.SetActive(false);
  107.         }
  108.     }
  109.  
  110. }
  111.  
Add Comment
Please, Sign In to add comment