Hi everybody!
I have a ship in my game it has port and right board situated guns. Guns search for the targets in array with tag
GameObject[ ] targets = GameObject.FindGameObjectsWithTag(enemyTag); Tag = Enemy
But I recenty found, that in a condition whenmy ship is attacked not from 1 side bu t from both sides guns still looking for the only nearest and half of guns “shoot” through the ship.
I haven’t managed to come up with anything but for splitting the tag by parts like Enemy+PortBoard etc. Taking in consideration that there are 300 ships in the game with standard behaviour - it is not possible to make 180 000+ tags. So it must be a variable of type String or a number composed automatically. But I have not found a way to form the enemy array by a variable.
Of course it is wrong but I need something like this:
GameObject[ ] targets = GameObject.FindGameObjectsWithString(Friend1);
GameObject[ ] targets = GameObject.FindGameObjectsWithVar(1000001);
Please advise.
So this variable is on an attached MonoBehaviour script, right? The best way is you maintain a list of this script attached to all your ships. Whenever you add a ship you add it to the list, whenever you remove a ship you remove it from the list. Then you just iterate the list. Much faster than calling any of the Find methods all the time, or looping through the list calling GetComponent on each one.
So attached to the ships something like this
public class ScriptWithVariable : MonoBehaviour
{
public string MyString;
}
Elsewhere you maintain a central list of all the attached ScriptWithVariable components
public List<ScriptWithVariable> Ships;
And when you want to find a specific ship
//Find a specific ship based on a string
//Returns null if the ship doesn't exist
public GameObject ReturnCorrectShip(string correctShipString)
{
GameObject returnValue = null
foreach (ScriptWithVariable swv in Ships)
{
if (swv.MyString == correctShipString)
{
returnValue = swv.gameObject;
}
}
return returnValue;
}
Then you just got to make sure you maintain the list, which is pretty easy. After you instantiate a ship you add it to the list, and in an OnDestroy method (or whatever method you call when you remove a ship from the game) you remove it from the list.
1 Like
Can’t you set a shooting range for each ‘cannon’. A large collider as trigger.
Use the OnCollisonEnter to trigger a method wich adds the particular GameObject with tag “enemy” to a List.
Use the OnCollsionExit to remove that particular GameObject from that List when it gets out of range.
Each cannon should automaticcally shoot at the closest partrtcular enemy ship in the List. Wich one, could be determined by the .magnitude of the Vector beween cannon and enemyship position.
Thanks everybody who answered. I tried to solve the probleme
https://vimeo.com/380812824
by tags. But nothing happens. Guns still aim the opposite board aims. Here is the script. The best way to cope with probleme is to switch the script off. BUT EVEN THIS DOES NOT WORK. The guns aim with the script switched off.
- protected IEnumerator FindClosestTarget()
- {
- while (true)
- {
- Transform closest = null;
- GameObject[ ] targets = GameObject.FindGameObjectsWithTag(«Enemy2»);
- float distance = sqrVisionRadius;
- foreach (GameObject go in targets)
- {
- Vector3 diff = go.transform.position - transform.position;
- float curDistance = diff.sqrMagnitude;
- if (curDistance < distance)
- {
- closest = go.transform;
- distance = curDistance;
- }
- }
- target = closest;
- nearest = target.name;
- yield return new WaitForSeconds(searchTimeDelay);
- }
- }