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 RangeChecker : MonoBehaviour {
- //A list of tags to check for
- public List<string> tags;
- //List of targets that is generated when we check for what has what tags
- List<GameObject> m_targets = new List<GameObject>();
- //When something enters the collider
- private void OnTriggerEnter(Collider other)
- {
- //When something enters the trigger set it as not a valid target instead of assuming that it is valid
- bool invalid = true;
- for (int i = 0; i < tags.Count; i++)
- {
- //Check if it has any of the tags from our list if so move on to the line where it adds the object to our valid target list
- if (other.CompareTag(tags[i]))
- {
- invalid = false;
- break;
- }
- }
- //If it is still invalid move on and wait for new objects to enter
- if (invalid)
- {
- return;
- }
- m_targets.Add(other.gameObject);
- }
- //When it exits the collider
- private void OnTriggerExit(Collider other)
- {
- for (int i = 0; i < m_targets.Count; i++)
- {
- //If the object leaving is a target remove it from the target list
- if (other.gameObject == m_targets[i])
- {
- m_targets.Remove(other.gameObject);
- return;
- }
- }
- }
- //When we say GetValidTargets we mean set the list of valid targets to the current list of targets
- public List<GameObject> GetValidTargets()
- {
- for (int i = 0; i < m_targets.Count; i++)
- {
- if (m_targets[i] == null)
- {
- m_targets.RemoveAt(i); i--;
- }
- }
- return m_targets;
- }
- public bool InRange(GameObject go)
- {
- //For each object inrange check if it is in the list of valid targets
- for (int i = 0; i < m_targets.Count; i++)
- {
- if (go == m_targets[i])
- {
- //If so then return the value to be true
- return true;
- }
- }
- //Otherwise it's false
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement