Alright, in my game I have a Runner object that moves at a velocity towards the left towards platforms. I have succeeded in spawning a random platform prefab at a pt a semi-random distance away from the last platform.
I do this by spawning the platform a random distance spacing, which the distance the object has moved since last platform was spawned.
public GameObject[] spawnableObjects;
private GameObject prefab;
private float spacing; // spawns every 2 units/metres
public float minY, maxY, minGap, maxGap;
private float oldX = 0; // previous camera position
private float newX = 0; // current camera position
private float distanceTravelled = 0;
private Vector3 spawnPos;
public Transform player; // getting camera reference
public Transform spawnPt;
private float currentY, lastY, currentX, lastX;
void Start()
{
//set beginning y pos
GameEventManager.GameStart += GameStart;
GameEventManager.GameOver += GameOver;
spacing = 20; //random between length of last prefab and max gap?
lastY = 0;
lastX = spawnPt.localPosition.x; //first spawn x pos
}
void Update () {
//Debug.Log (distanceTravelled);
newX = player.transform.localPosition.x;
distanceTravelled += newX - oldX; // if you are goint to the right
// if you're going to the left, do this instead:
// distanceTravelled += oldX - newX;
oldX = newX; // this is very important
if (distanceTravelled >= spacing) {
distanceTravelled = 0;
spawnNewObject ();
}
}
void spawnNewObject () {
// code to spawn. Use the newX + some X offset while declaring your new object's position.
int rand = Random.Range (0, spawnableObjects.Length);
prefab = spawnableObjects[rand];
currentY = lastY + Random.Range(minY, maxY);
currentX = lastX + (prefab.transform.localScale.x + 5f); //randomize 5, 5 is gap
spawnPos = new Vector3((spawnPt.localPosition.x), currentY,0f);
//Debug.Log ("new x:"+currentX);
Instantiate (prefab, spawnPos, Quaternion.identity);
lastY = currentY;
lastX = currentX;
spacing = Random.Range (prefab.transform.localScale.x+3, prefab.transform.localScale.x+5);
Debug.Log("spawn now!! spacing="+spacing);
}
The spawnpt
game object moves at the same acceleration/speed as Runner, so it appears as if platforms are moving towards the Runner.
For some reason, the Random.Range (prefab.transform.localScale.x+3, prefab.transform.localScale.x+5);
which is the length of the prefab currently being spawned+3 or +4 makes it so the platforms virtually never touch.
My thinking in doing length was if the new platform is being spawned from the empty spawnPt.localPosition.x
, if the spacing was atleast the length of the current prefab, they wouldn't touch.
Obviously something is flawed in my logic as if I just do prefab.transform.localScale.x
platforms touch. I need a new approach for this.
How can I have my randomly selected prefab spawn with a randomized GAP between itself and the last prefab that was spawned? I have tried everything including Physics spheres (don't want to go that route) but can't create a gap that I can alter.
Is there a way to do this?
Aucun commentaire:
Enregistrer un commentaire