- Home /
Question by
thekrocker · Aug 15, 2021 at 06:06 PM ·
index
My Monster doesn't go for others if not in range
Greetings, In my code, as you see Monster tries to find 0 index. If so, goes there and kill. then it will also go for others because we remove from the list. My problem is here, If we move away (thats the purpose) 0 indexed innocent, it wont look for other innocent in range. I tried to increase Index but in time, i get an index error.
private void Start()
{
_gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
StartCoroutine(MoveCoroutine());
}
IEnumerator MoveCoroutine()
{
yield return new WaitForSeconds(3);
while (true)
{
MoveToInnocents();
yield return null;
}
}
private void MoveToInnocents()
{
Debug.Log(_innocentIndex);
if (_gameManager.İnnocentList.Count > 0)
{
var targetPosition = _gameManager.İnnocentList[_innocentIndex].transform.position;
if (Vector3.Distance(transform.position, targetPosition) < range)
{
transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, targetPosition) < 0.3f)
{
_gameManager.DestroyInnocent(_innocentIndex);
}
}
else if(Vector3.Distance(transform.position, targetPosition) > range)
{
Debug.Log("Out of Range");
}
}
}
Comment
Your answer
