- Home /
instantiate objects from right and left of screen to center
Im trying to have objects from either side of the screen one at a time and meet in the middle of the screen on top of each other so a player can jump on them one at a time! i have so far made a empty game object twice and put either side of the screen to instantiate the objects and i have also attached 2 different scripts to them! when the first object is spawn it goes to the center and the second object from the left is delayed with a coroutine and then goes to the center! i want to be able to keep this processing going higher but unsure how without creating many game objects to keep spawning! ive alos noticed some strange behavior! when i play in editor with screen maximised the object on the right does not work but does on small view!
public class CrateSpawner : MonoBehaviour {
public GameObject crate;
Vector3 movePosition = new Vector3(0f, .5f, 0f);
public float speed = 5f;
void Start()
{
SpawnCrate ();
}
void Update()
{
if(crate.transform.position != movePosition)
{
Vector3 newPos = Vector3.MoveTowards(crate.transform.position, movePosition, speed * Time.deltaTime);
crate.transform.position = newPos;
}
}
public void SpawnCrate()
{
crate = Instantiate(crate, transform.position, transform.rotation)as GameObject;
}
}
script to spawn object from the left side of screen..
public class CrateLeftSpawner : MonoBehaviour {
public GameObject crate;
Vector3 movePosition = new Vector3(0f, 1.5f, 0f);
public float speed = 5f;
void Start()
{
//SpawnCrate ();
StartCoroutine("SpawnDelay");
}
void Update()
{
if(crate.transform.position != movePosition)
{
Vector3 newPos = Vector3.MoveTowards(crate.transform.position, movePosition, speed * Time.deltaTime);
crate.transform.position = newPos;
}
}
IEnumerator SpawnDelay()
{
yield return new WaitForSeconds(2);
crate = Instantiate(crate, transform.position, transform.rotation)as GameObject;
}
}
Your answer
Follow this Question
Related Questions
When the game starts, how can I instantiate a ball onto a random client? 0 Answers
Spawning Enemies after a Player reaches a certain score 1 Answer
How can I instantiate a unique game object? 2 Answers
How to spawn objects using an array of spawn points w/o intersecting 1 Answer
Multiple spawn location with random game object in random time spawn c# 1 Answer