Calculate velocity vector for spawning prefabs at player
I have a few spawner objects that spawn prefabs with a given velocity at creation (so they are "shooting" towards the player). At the moment this works fine as long as the spawner objects are in line with the player, so inside a pretty small range of x position values.
What I want is to have each spawner calculate its own velocity vector to be able to spawn the objects shooting towards the player.
At the moment my spawners have the following code to spawn its objects:
IEnumerator SpawnFood()
{
yield return StartCoroutine(Wait());
while (true)
{
GameObject go = Instantiate(foodPrefabs[Random.Range(0, foodPrefabs.Length)]);
Rigidbody temp = go.GetComponent<Rigidbody>();
temp.velocity = new Vector3(0f, Random.Range(5f,12f), -15f);
temp.angularVelocity = new Vector3(1f, 0f, 1f);
temp.useGravity = true;
Vector3 startPos = transform.position;
startPos.x += Random.Range(-1f, 1f);
go.transform.position = startPos;
//if (Player.Score > 10)
//{
// waitTime = waitTime - 1f;
//}
yield return new WaitForSeconds(waitTime);
}
}
Comment
Your answer