Language

How can i check if one object gets near any object of a prefab

Hi im trying to make a simple code so a object hides when it comes near a trench, the problem is that I have many trenches and the way I've done it it only focuses the first one, but i want it to focus all the objects of a prefab. Please help, im new on this and i could only think of doing it this way:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class AIsoldier : MonoBehaviour { public float speed = 0.1f; public Transform Trench; public float DistanceToTrench; public bool InTrench; // Use this for initialization void Start () { }

2 Replies

You could have this a little easier. Add a spherecollider as trigger to your trenches. Add OnTriggerEnter/ Exit to your code. Whenever you enter one, you count an int up, when you exit one, you count down. When the count is 0, you're in no range and vice versa. Easy.