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 () {
}
// Update is called once per frame
void Update() {
DistanceToTrench = Vector2.Distance(transform.position, Trench.position);
Vector2 position = this.transform.position;
position.x += speed;
if (DistanceToTrench < 0.1 && InTrench == false)
{
InTrench = true;
speed = 0;
position.y += -5f;
}
else if (Input.GetKeyDown(KeyCode.Space) && InTrench==true)
{
InTrench = false;
speed = 0.1f;
position.y += 5f;
position.x += 0.1f;
}
this.transform.position = position;
}
}