How to instantiate object dependent on position
Hi, I'm trying to make clouds system for my android game, and it should work like this, if player position is greater than 10 start generating clouds, and generate next one if player y position is grater than last generated cloud + 10, but idk how to do this :< I've already tried timers, for, and do while loops, and anything isn't working, clouds are generating each frame. Here's code for basic generating:
Answer by Dibbie · Jun 22, 2016 at 05:06 AM
Firstly, NEVER run any type of "loop" inside an Update, BECAUSE that it runs every frame, youd essentially be trying to run a loop... Every frame, which is performance wise, essentially saying "goodbye" to your players CPU, and yours, because of the amount of resources its using to do something unnecessary. (and doing that, Unity will probably crash on you)
Second, what you want to do is a little bit of whats known as "Procedural Generation" its when the code figures out how to make something look endless or seamless without the assistance of a human or manual copy/paste essentially...
All you need to achieve it, is something like this - and this is all untested C# code...
//Global variables
public GameObject cloud; //the object you want to be instantiated over and over
public GameObject player; //reference to your player
public float distance = 10f; //the minimal distance each cloud should be apart from eachother
public float timeframe = 3f; //the amount of time before the next cloud should be spawned
private Transform lastCloud; //save the location of the last cloud for the next one to spawn
void GenerateClouds(){
if(lastCloud == null){
lastCloud = (GameObject) Instantiate(cloud,new Vector3(10f,0,0), cloud.rotation);
} else {
lastCloud = (GameObject) Instantiate(cloud,new Vector3(lastCloud.position.x + distance, 0, 0), cloud.rotation);
}
}
//----------------------------------------------------------------------------------------
//In Update you can use it like this:
void Update(){
if(player.transform.position.x > distance){
Invoke("GenerateClouds", timeframe);
}
//----------------------------------------------------------------------------------------
//Or you can use it in Start instead, like this:
void Start(){
InvokeRepeating("GenerateClouds",timeframe,timeframe);
}
//If you use it in Start instead, you just have to surround the GenerateClouds function with that if-statement, so it would then look like this:
void GenerateClouds(){
if(player.transform.position.x > distance){
if(lastCloud == null){
lastCloud = (GameObject) Instantiate(cloud,new Vector3(10f,0,0), cloud.rotation);
} else {
lastCloud = (GameObject) Instantiate(cloud,new Vector3(lastCloud.position.x + distance, 0, 0), cloud.rotation);
}
}
}
If im understanding your question right, that should achieve what you want, given, youll most likely have to tweak it a bit to work for your game, but mostly things like value changes, that is, if you choose to copy/paste rather than use that script as a reference or guide for building your own, that may be more efficient for your game.
Oh man, you're an angel, i've just asked for logic, not whole implementation, thank you. I'll try to do something with this, because it's not working anyway(now spawning clouds, and if i try debug.log in if/else it doesn't appear) But thank you anyway :D
No problem, if you found my answer helpful as well, please give it an upvote or mark it as best answer =)