Question by
Michaeldearmas · Dec 29, 2020 at 08:34 PM ·
movementvector3coroutinepathfindinglists
Vector 3 MoveTowards not working in my coroutine
So I am trying to iterate through a list of tiles on keypress, each keypress the character moves towards the next tile in the list. Currently I trigger the tile and then take in the list of tiles connected to the parent tile (with the rigidbody) in the scene. Then I start the couroutine.
Right now I simply stay in place and do not go anywhere on keypress, have tried getkeyaxisraw and space.
Anybody have any ideas or suggestions?
Thanks!
YellowTileColl yellowTileScript;
public GameObject yellowTGO;
private List<GameObject> yellowTiles;
int startPoint;
bool hasEnteredYT;
float speed;
Movement movementScript;
IEnumerator currentRoutine;
private Vector3 destination;
// Start is called before the first frame update
void Start()
{
yellowTileScript = GameObject.Find("YellowTile").GetComponent<YellowTileColl>();
yellowTiles = new List<GameObject>();
yellowTiles = yellowTileScript.childrenY;
movementScript = GetComponent<Movement>();
float speed = 10f;
}
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (hasEnteredYT == true)
{
if (currentRoutine != null)
{
StopCoroutine(currentRoutine);
}
currentRoutine = YellowMovement(yellowTiles, startPoint);
StartCoroutine(currentRoutine);
Debug.Log("has entered");
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
hasEnteredYT = true;
for (int i = 0; i < yellowTiles.Count; i++)
{
if (yellowTiles[i] == collision.gameObject)
{
startPoint = i;
}
}
}
IEnumerator YellowMovement(List<GameObject> tiles, int start)
{
start += 1;
destination = tiles[start].transform.position;
while (transform.position != destination)
{
Debug.Log(start);
Debug.Log(destination);
transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
yield return null;
}
}
}
Comment