I want to Spawn Some Herbs in my game in 3 separate positions left, right, and center but they spawn in one position on top of each other.
These are the scripts for the spawning:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour
{
public Transform[] spawnLocations;
public GameObject[] whatToSpawnPrefab;
public GameObject[] whatToSpawnClone;
void spawnSomethingAwesomePlease()
{
whatToSpawnClone[0] = Instantiate(whatToSpawnPrefab[0], spawnLocations[0].transform.position, Quaternion.Euler(0,0,0)) as GameObject;
}
}
This is the HerbSpawner:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HerbSpawner : MonoBehaviour
{
//Here, we declare variables.
public Transform groupTransform;
public GameObject[] allObjsToSpawnFrom;
Vector2 spawnPos;
void Start()
{
spawnPos = Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 0.5f));
}
void Update()
{
// let's also spawn on button press:
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.GetRayIntersection(
Camera.main.ScreenPointToRay(Input.mousePosition));
if (hit.collider && hit.collider.CompareTag("Bush"))
{
for (int i = 0; i < 3; i++) SpawnRandomly();
}
}
}
void SpawnRandomly()
{
int find = Random.Range(0, allObjsToSpawnFrom.Length);
SpawnIt(allObjsToSpawnFrom[find]);
}
void SpawnIt(GameObject objToSpawn)
{
Vector2 spawnPos =
Camera.main.ViewportToWorldPoint(new Vector2(0.5f, 0.7f));
Instantiate(objToSpawn, spawnPos, Quaternion.identity, groupTransform);
}
}