Hi! I need help cleaning up my code so I can be sure that it works properly and efficiently. I have been doing research and kind of grabbing things from various examples and it isn’t the best right now. I think most of my confusion comes from arrays and how to interact with them. They are probably very simple answers.
I currently have a node based cover system that works pretty well. The AI finds the game objects tagged as “cover” and does various tasks such as shooting raycasts to targets and measuring distances then assigning point values to pick the “best” one.
The problem is that I have to manually place each cover node by hand.
I decided to try and have the AI spawn a grid of nodes around them.
I am making a grid of points like this
using UnityEngine;
using System.Collections;
public class GridMaker : MonoBehaviour
{
public int x = 5;
public int z = 5;
public float spacing = 5f;
public float upDistance = 2f;
public GameObject prefab;
void Start()
{
Vector3 pos = (transform.position);
GameObject[,] my2DArray = new GameObject[x,z] ;
for(int i = 0;i <x;i++)
{
for(int j = 0;j < z;j++)
{
my2DArray[i,j] = Instantiate(prefab, new Vector3(j * spacing + pos.x + spacing, pos.y + upDistance, i * spacing + pos.z + spacing), transform.rotation) as GameObject;
my2DArray[i,j] = Instantiate(prefab, new Vector3(j * -spacing + pos.x, pos.y + upDistance, i * -spacing + pos.z), transform.rotation) as GameObject;
my2DArray[i,j] = Instantiate(prefab, new Vector3(j * spacing + pos.x + spacing, pos.y + upDistance, i * -spacing + pos.z), transform.rotation) as GameObject;
my2DArray[i,j] = Instantiate(prefab, new Vector3(j * -spacing + pos.x, pos.y + upDistance, i * spacing + pos.z + spacing), transform.rotation) as GameObject;
}
}
}
}
I want them to be along the ground so I made a separate script that shoots rays and moves the position to the impact. I attached this script to the cover node game object and execute it during the start function.
it just looks like this
void Start()
{
Vector3 dir = transform.TransformDirection(-Vector3.up);
RaycastHit hit;
if (Physics.Raycast(transform.position, dir, out hit, downRange))
{
transform.position = hit.point;
}
StartCoroutine (Kill ());
}
The first thing I am wondering is if there is a way I can just make the objects do this without having to do it on a separate script.
The next thing I am wondering is this.
I plan on merging the grid creator script within my current GetCover.
After the grid is created I am using FindObjectsWithTag to create an array that is used for “sorting” and finding the best one.
Two things concern me. I am wondering if I have to keep the scripts above separate (the generator and the ground finder) if when the array goes to sort the nodes, it will execute before the nodes have found the ground, shooting raycasts and measuring distances from the incorrect spot. I want to make sure this does not happen.
The second issue that I have is the FindObjectsWithTag, I am wondering how I would go about it so the AI only uses its own generated grid and doesn’t use another’s if multiple AI’s are trying to find cover at the same time. I have the nodes set up to only last five seconds but that is more than enough time to possibly kill performance.
I know it’s a lot of questions, but can anyone help me out please?