Прежде всего, вы не можете иметь несколько Renderers
, прикрепленных к одному и тому же GameObject
.
Так что я бы предпочел просто создать 4 пустых дочерних объекта с прикрепленными к ним компонентами, такими как
void Start()
{
lr = new GameObject().AddComponent<LineRenderer>();
lr.gameObject.SetParent(transform, false);
// just to be sure reset position and rotation as well
lr.gameObject.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
lr2 = new GameObject().AddComponent<LineRenderer>();
lr2.gameObject.SetParent(transform, false);
// just to be sure reset position and rotation as well
lr2.gameObject.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
lr3 = new GameObject().AddComponent<LineRenderer>();
lr3.gameObject.SetParent(transform, false);
// just to be sure reset position and rotation as well
lr3.gameObject.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
lr4 = new GameObject().AddComponent<LineRenderer>();
lr4.gameObject.SetParent(transform, false);
// just to be sure reset position and rotation as well
lr4.gameObject.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
}
Проще было бы, если бы вы создали один префаб из пустого GameObjet с LinerRenderer
и сделали, например,
LineRenderer linePrefab;
void Start()
{
lr = Instantiate(linePrefab, transform);
lr2 = Instantiate(linePrefab, transform);
lr3 = Instantiate(linePrefab, transform);
lr4 = Instantiate(linePrefab, transform);
//...
}
или ... почему бы не создать эту структуру в редакторе?Чем бы вам не пришлось делать это по сценарию вообще ...
Вы никогда не заполните свой список lines
(на самом деле вам даже не понадобятся отдельные переменные)
lines = new List<LineRenderer>();
lines.Add(lr);
lines.Add(lr2);
lines.Add(lr3);
lines.Add(lr4);
и
// МНОЖЕСТВО ПУЧКОВ, КОТОРЫЕ РОЗОВЫЕ
Вы должны назначить Material
для LineRenderer
s, например, как
public Material lineMaterial;
void Start()
{
// ...
// set material
foreach(var line in lines)
{
line.material = lineMaterial;
// could also give them colors
//line.material.color = someColor;
}
}
Теперь к регистрации Update()
// ...
// run through all available lines
for (int i = 0; i < lines.Count; i++)
{
var currentLine = lines[i];
// if under player count -> set enabled and set positions
if(i < amountofplayers)
{
currentLine.enabled = true;
// set target to player at same index as this line
currentLine.SetPosition(0, transform.position);
currentLine.SetPosition(1, UsersInsideZone[i].gameObject.transform.position);
}
// otherwise set disabled
else
{
currentLine.enabled = false;
}
}
Примечание : я бы все равно не сделал это Physics.OverlapSphere
, так как сначала это стоитво-вторых, производительность может не возвращать игроков всегда в том же порядке.
Я бы предпочел сделать что-то вроде
private List<Collider> UsersInsideZone = new List<Collider>();
private void OnTriggerEnter(Collider col)
{
// you would give them tags instead of a layer
if(col.gameObject.tag != "player") return;
// or alternative set which layers can collide at all in the Physics settings
if(UsersInsideZone.Contains(col)) return;
UsersInsideZone.Add(col);
}
private void OnTriggerExit(Collider col)
{
// you would give them tags instead of a layer
if(col.gameObject.tag != "player") return;
// or alternative set which layers can collide at all in the Physics settings
if(!UsersInsideZone.Contains(col)) return;
UsersInsideZone.Remove(col);
}
private void Update()
{
// run through all available lines
for (int i = 0; i < lines.Count; i++)
{
var currentLine = lines[i];
// if under player count -> set enabled and set positions
if(i < amountofplayers)
{
currentLine.enabled = true;
// set target to player at same index as this line
currentLine.SetPosition(0, transform.position);
currentLine.SetPosition(1, UsersInsideZone[i].gameObject.transform.position);
}
// otherwise set disabled
else
{
currentLine.enabled = false;
}
}
}