I’ve been making a 2-team deathmatch networked shooter, and am almost done. I’ve tried to make an algorithm for this, but i’ve been seeing some errors and cannot find out where the bugs are. What I want is this:
An algorithm that cycles through all existing NetworkStartPositions, and gives you the position of the network start position farthest from an enemy, so the player may respawn. How I thought it would go is:
-For loop cycles through all spawn points
-Then cycles through all enemy players within that for loop
-Finds the closest enemy player to each individual spawn point, and saves it in a script component attached to each gameobject holding a networkstartposition component
-Compares those distances to find the farthest one to any spawn point (essentially the farthest closest player, haha)
-Takes the transform.position of the spawn point with the largest number from this algorithm
-Sets the player transform.position to that Vector3
The main bug is that players can respawn in the same spot over and over again despite there being clearly more optimal positions, and sometimes will respawn somewhere and instantly move somewhere completely different once they begin to walk (Although I believe the 2nd one might be caused by networking issues, the main one is the first problem)
I cannot find out whether or not the issue is my code or the algorithm, so I would like it if someone could either edit my code or write a new algorithm, thanks in advance
Script Attached to NetworkStartPosition GameObjects:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ClosestPlayerDistances : NetworkBehaviour {
public float closestDistanceTeam1 = 1000.0f;
public float closestDistanceTeam2 = 1000.0f;
public void SetDistances()
{
for(int i = 0; i<GameObject.FindGameObjectsWithTag("Player").Length; i++)
{
if(GameObject.FindGameObjectsWithTag("Player")*.GetComponent<PlayerControllerBackup>().teamnum==1)*
-
{*
_ if(Distance(GameObject.FindGameObjectsWithTag(“Player”).transform.position,transform.position)<closestDistanceTeam1)_
* {*
_ closestDistanceTeam1=Distance(GameObject.FindGameObjectsWithTag(“Player”).transform.position,gameObject.transform.position);
* }
}
else if(GameObject.FindGameObjectsWithTag(“Player”).GetComponent().teamnum==2)
{*_
_ if(Distance(GameObject.FindGameObjectsWithTag(“Player”).transform.position,transform.position)<closestDistanceTeam2)
* {*_
_ closestDistanceTeam2=Distance(GameObject.FindGameObjectsWithTag(“Player”).transform.position,gameObject.transform.position);
* }
}
}
}*_
* private float Distance(Vector3 first, Vector3 second)*
* {*
* return(Mathf.Sqrt(Mathf.Pow(first.x-second.x,2)+Mathf.Pow(first.y-second.y,2)+Mathf.Pow(first.z-second.z,2)));*
* }*
}
Health / Respawn Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Health : NetworkBehaviour {
* public const int maxHealth = 200;*
* [SyncVar]*
* public int currentHealth = maxHealth;*
* [SyncVar]*
* public bool dead = false;*
* public float deathTime = 3.0f;*
* private float distance = 0.0f;*
* private Vector3 spawnPosition;*
* private float timer = 0.0f;*
* void Update()*
* {*
* if(!isLocalPlayer)*
* {*
* return;*
* }*
* GameObject.Find(“Health Count”).GetComponent().text = currentHealth.ToString();*
* if(dead)*
* {*
* timer += Time.deltaTime;*
* if(timer >= deathTime)*
* {*
* timer = 0.0f;*
* gameObject.GetComponent().ResetAmmo();*
* CmdResetHealth();*
* RpcRespawn();*
* GameObject.Find(“Ammo Count”).GetComponent().text = (“20/20”);*
* }*
* }*
* }*
* [Command]*
* public void CmdTakeDamage(int amount)*
* {*
* if(!isServer)*
* {*
* return;*
* }*
* if(currentHealth>0)*
* {*
* currentHealth -= amount;*
* }*
* if(currentHealth <= 0 && !dead)*
* {*
* currentHealth = 0;*
* dead = true;*
* if(gameObject.GetComponent().teamnum==1)*
* {*
* GameObject.Find(“Game Controller”).GetComponent().CmdIncreaseScore(2);*
* }*
* else if(gameObject.GetComponent().teamnum==2)*
* {*
* GameObject.Find(“Game Controller”).GetComponent().CmdIncreaseScore(1);*
* }*
* }*
* }*
* [ClientRpc]*
* public void RpcRespawn()*
* {*
* if(!isLocalPlayer)*
* {*
* return;*
* }*
* distance = 0.0f;*
* spawnPosition = Vector3.zero;*
* for(int i = 0; i<GameObject.FindObjectsOfType().Length; i++)*
* {*
_ GameObject.FindObjectsOfType().gameObject.GetComponent().SetDistances();_
* if(gameObject.GetComponent().teamnum==1)*
* {*
_ if(GameObject.FindObjectsOfType().gameObject.GetComponent().closestDistanceTeam2>distance)
* {*_
_ distance = GameObject.FindObjectsOfType().gameObject.GetComponent().closestDistanceTeam2;
spawnPosition = GameObject.FindObjectsOfType().transform.position;
* }
}
else if(gameObject.GetComponent().teamnum==2)
{*_
_ if(GameObject.FindObjectsOfType().gameObject.GetComponent().closestDistanceTeam1>distance)
* {*_
_ distance = GameObject.FindObjectsOfType().gameObject.GetComponent().closestDistanceTeam1;
spawnPosition = GameObject.FindObjectsOfType().transform.position;
* }
}
}
transform.position = spawnPosition;
}*_
* [Command]*
* private void CmdResetHealth()*
* {*
* if(!isServer)*
* {*
* return;*
* }*
* currentHealth = maxHealth;*
* dead = false;*
* }*
}