This problem I imagine is easy to fix but I can’t seem to figure out how to fix the problem.
Basically I’m spawning a bunch of objects with a for statement around the player and then i want it to move those objects towards the player. I created this function to try and do that:
public IEnumerator CirclePellet()
{
Vector2 playerPosition = player.transform.position;
textManager.DisableTextBox();
for(int i = 0; i < 25; i++)
{
Vector2 pos = CirclePlayer(playerPosition, 3.0f);
Quaternion rot = Quaternion.FromToRotation(Vector2.down, playerPosition-pos);
pellet = Instantiate(PelletCircle, pos, rot) as GameObject;
yield return new WaitForSeconds(0.1f);
}
yield return new WaitForSeconds(1f);
pellet.transform.position = Vector2.MoveTowards(pellet.transform.position, playerPosition, 5 * Time.deltaTime);
}
I’ve also simply assignment the variables at the top of the script:
public GameObject player;
public GameObject PelletCircle;
public GameObject pellet;
This is also the function I calling to get the position:
Vector2 CirclePlayer(Vector2 centre, float range)
{
float ang = Random.value * 360;
Vector2 pos;
pos.x = centre.x + range * Mathf.Sin(ang * Mathf.Deg2Rad);
pos.y = centre.y + range * Mathf.Cos(ang * Mathf.Deg2Rad);
return pos;
}
So the spawning of the objects works fine and they all spawn around the player but the one thing I can’t get them to do is move towards the player and I don’t know why. Does anyone know what I’m doing wrong?
Thanks