Zdravím
Dlho sa trápim s problémom ako náhodne umiestniť nepriateľov na body,ktoré u mňa tvoria gule? Pri spustení mi objekty umiestni náhodne ale niekedy na nesprávnom mieste,aj tam kde nemám body:
Ďakujem za rady.Tu je kód
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using Random = UnityEngine.Random;
public class Randomplace : MonoBehaviour {
public int minNumber =0;
public GameObject[] wayPoints;
public GameObject[] enemys;
Vector3 pos;
private List<int> _validNumbers;
private int number;
private void Awake()
{
_validNumbers = new List<int>();
for (int i = minNumber; i <= wayPoints.Length; i++)
_validNumbers.Add(i);
}
void Start()
{
for (int j=0; j<enemys.Length;j++) {
if(enemys[j].activeInHierarchy) {
enemys[j].SetActive(true);
number = GetRandomNumber();
GameObject waypoint = wayPoints[number];
Vector3 pos= waypoint.transform.position;
enemys[j].transform.position = pos;
Debug.Log( "zomby su na cislach "+wayPoints[number]+"x"+ wayPoints[number].transform.position.x +
"z"+wayPoints[number].transform.position.z+"vola"+enemys[j]);
}
}
}
private void Update()
{
}
private int GetRandomNumber()
{
var nextIndex = Random.Range(0, _validNumbers.Count - 1); //
var result = _validNumbers[nextIndex];
_validNumbers.RemoveAt(nextIndex);
return result;
}
}