So my player is moving along the Y axis downwards but not at a constant or predictable rate (meaning the player can even stop moving ). The camera is following the player so I decided to use it to set a location for spawning game objects along the y axis. I used a Yoffset to make it spawn objects ahead of the camera and a DifferenceInDistance variable to try and maintain a distance between game objects, however, I’m still having game objects appearing at the same position. Please Help thanks ![]()
public class ObjectSpawnner : MonoBehaviour {
public float Yoffset;
public GameObject [] obstacles;
public int NumberOfGameObjects;
public float startTime;
public float RepeatRate;
public float DifferenceInDistance = 4;
float YDistance = 0;
// Use this for initialization
void Start () {
InvokeRepeating ("GenerateRandomObjects",startTime ,RepeatRate );
}
void GenerateRandomObjects(){
float x =obstacles [0].transform.position.x;
float y = GameObject.Find ("Main Camera").transform.position.y;
int r = Random.Range (0,NumberOfGameObjects);
YDistance += DifferenceInDistance;
Debug.Log ("Y distance is " + YDistance );
Instantiate (obstacles [r], new Vector3 (x, y - Yoffset - YDistance, 0), Quaternion.identity);
}
}
