So what i am trying to do is make a script that tracks the player’s movement and spawns in front of him random prefab obstacles around 20 units ahead of him (just out of his line of sight) and I am not sure how to do that since the current script only spawns the object at the initial location of the player and that’s all.
Second thing would be how to spawn the ground for the player a few units before he reaches the end of the first ground.
public GameObject prefab1, prefab2, prefab3, prefab4, prefab5;
public float SpawnRate = 0f;
public GameObject myPlayer;
private Vector3 GetXAxis()
{
return new Vector3(myPlayer.transform.position.x, 0, 0);
}
private void Update()
{
Vector3 xVector = GetXAxis(); // getting that vector from above
}
public float nextSpawn = -3f;
int WhatToSpawn;
private void FixedUpdate()
{
if (Time.time > nextSpawn)
{
WhatToSpawn = Random.Range(1, 3);
Debug.Log(WhatToSpawn);
switch (WhatToSpawn)
{
case 1:
Instantiate(prefab1, transform.position = GetXAxis() * Time.fixedDeltaTime * 20 , Quaternion.identity);
break;
case 2:
Instantiate(prefab2,GetXAxis() * 20 * Time.fixedDeltaTime , Quaternion.identity);
break;
/* case 3:
Instantiate(prefab3, transform.position, Quaternion.identity);
break;
case 4:
Instantiate(prefab4, transform.position, Quaternion.identity);
break;
case 5:
Instantiate(prefab5, transform.position, Quaternion.identity);
break;
*/
}
nextSpawn = Time.time + SpawnRate;
}
}
}