J'aurais besoin d'un conseil sur les coroutine. Je comprend comment les utiliser temps que tous se passe dans la même classe mais, je ne comprend pas comment les utiliser quand ça se passe dans deux classes. L'une lançant la coroutine et l'autre la méthode a utiliser dedans.
par exemple :
Une class Ennemy :
Code : Tout sélectionner
public class Ennemy : MonoBehaviour
{
private Transform _transf;
private Vector3[] _pathArray = new Vector3[]
{
Vector3.zero,
new Vector3(10, 0, 0),
new Vector3(20, 0, 0),
new Vector3(30, 0, 0)
};
private float _speed = 5f;
private void Awake()
{
_transf = transform;
_transf.position = _pathArray[0];
}
public void MoveTo()
{
_transf.position = _pathArray[0];
int _targetPathIndex = 1;
Vector3 _targetPath = _pathArray[_targetPathIndex];
Vector3 _EndPath = _pathArray[3];
while(_transf.position != _EndPath)
{
_transf.position = Vector3.MoveTowards(_transf.position, _targetPath, _speed * Time.deltaTime);
if(_transf.position == _targetPath)
{
_targetPathIndex = _targetPathIndex + 1;
if (_targetPathIndex > 3) break;
_targetPath = _pathArray[_targetPathIndex];
}
}
}
Code : Tout sélectionner
public class EnnemyManager : MonoBehaviour
{
public Ennemy ennemy;
private void Update()
{
StartCoroutine(EnnemyMove());
}
IEnumerator EnnemyMove()
{
///
}
}
Code : Tout sélectionner
public IEnumerator CoroutineMoveTo()
{
//start
_transf.position = _pathArray[0];
int _targetPathIndex = 1;
Vector3 _targetPath = _pathArray[_targetPathIndex];
Vector3 _EndPath = _pathArray[_pathArray.Length - 1];
while (_transf.position != _EndPath)
{
_transf.position = Vector3.MoveTowards(_transf.position, _targetPath, _speed * Time.deltaTime);
if (_transf.position == _targetPath)
{
_targetPathIndex += 1;
if (_targetPathIndex > 3) break;
_targetPath = _pathArray[_targetPathIndex];
}
yield return null;
}
}
Bonne journée,