Instaniate not working with Random.Range
I'm making a game in which you tap to jump over obstacles.
I've got everything working except the randomizing of the distance between one obstacle and the next.
The objects are not being created at a far enough distance away from each other for them to be jumpable, Why won't this work:
void CreateOb() {
lastPos = 21f;
Vector3 cdsa = new Vector3(Random.Range(4 + lastPos, 16 + lastPos) , -0.53f, -5.42f);
GameObject ObstacleInstance;
ObstacleInstance = Instantiate (Obs, cdsa, rot.rotation) as GameObject;
lastPos = ObstacleInstance.transform.position.x;
I am using InvokeRepeating() in the Start() method to repeat this.
The objects just seem to stack up beside each other instead of being further away...
Please Help :)
using this in start():
InvokeRepeating ("CreateOb", 2f, 2f);
Answer by hexagonius · Sep 07, 2015 at 09:19 PM
pull this line out of the code block to somewhere earlier and you're good to go:
lastPos = 21f;
I haven't got time right now to test this, but will in about 15 hours so will update then, thanks!!!
Hmm... there still seems to be an issue @hexagonius, they are spawning better but are still spawning behind the lastPos?? trying to see why now
Isn't that what you wanted? lastPos is an ever increasing value if I'm not mistaken.
@hexagonius $$anonymous$$inda, I'm trying to get the objects to instaniate in front of (so object A at x 21, then next at say x 31) I can't figure out why. I think it is unable to get the X transform from the instaniated object - working at it now
Vector3 lastPos = new Vector3{21, -.53f, -5.42f};
void CreateOb() {
Vector3 cdsa = lastPos + Vector3.right * Random.Range(4 + lastPos, 16 + lastPos);
GameObject ObstacleInstance;
ObstacleInstance = Instantiate (Obs, cdsa, rot.rotation) as GameObject; lastPos = ObstacleInstance.transform.position;