I am trying to make this game object respawn in a random location for my 2D game. I am a newb to Unity and I really just want to learn some basic. Any help would be greatly appreciated. here is the Script im using
using UnityEngine;
using System.Collections;
public class JumpHazardFactory : MonoBehaviour {
public GameObject preFab;
float spawnTime;
float spawnFrequency = 2;
// Use this for initialization
void Start () {
spawnTime = Time.time + 3;
}
// Update is called once per frame
void Update () {
spawnTimer ();
}
void spawnTimer () {
if (Time.time >= spawnTime){
spawnHazard();
spawnTime = Time.time+spawnFrequency;
}
}
void spawnHazard(){
Instantiate (preFab);
}
after you have instantiated the preFab, you can change its position. create a new vector and give it a random X,Y,Z position and set the position of the preFab to the ranadom position.
For the Quaternion you can use the same as the prefab:
gameObject.transform.rotation;
But with Instantiate you are going to create many gameObjects of your prefab, and I don’t know if what you want to do is just relocate it in another position. If that’s what you want then you should just use Translate which receive the new coordinates in a Vector2(x, y).