Hi!
I’m trying to make mice spawn randomly at certain positions but I’m struggling to find a good way to do so.
I thought I could define the positions with a vector2 but the instantiate function obviously doesn’t work with that.
Then I also found out that I can only assign a transform.position value to a gameobject and thus can’t really set specific transform positions in code. Unless there is another way to do it?
Now I wonder if this idea of setting spawn positions in code is feasible or if I should just give up with this and use gameobjects, or use Random.Range on X and Y and somehow exclude all values except for the ones at my target positions.
I’ve been stuck on this for a while so some help would be greatly appreciated
public class MouseSpawnerScript : MonoBehaviour
{
public GameObject mouse1;
Vector2[] position ;
// Start is called before the first frame update
void Start()
{
Vector2 pos1 = new Vector2(0.9f, 0.7f);
Vector2 pos2 = new Vector2(0, 0.7f);
Vector2 pos3 = new Vector2(0.9f, 0.7f);
Vector2 pos4 = new Vector2(-0.9f, 0.1f);
Vector2 pos5 = new Vector2(0, 0.1f);
Vector2 pos6 = new Vector2(0.9f, 0.1f);
Vector2 pos7 = new Vector2(-0.9f, -0.5f);
Vector2 pos8 = new Vector2(0, -0.5f);
Vector2 pos9 = new Vector2(0.9f, -0.5f);
Vector2[] position = {pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9};
InvokeRepeating("Spawn", 1, 4);
}
// Update is called once per frame
void Update()
{
}
void Spawn()
{
Instantiate(mouse1, position[Random.Range(1,8)]);
}
}