Spawn origin point!!

Hi there… so I have created an object that spawns hazards continuously in a loop… you can control how wide the spawn length is… but you i can’t seem to move it around… here is the code i’ve written.

using UnityEngine;
using System.Collections;

public class CloudScript : MonoBehaviour
public GameObject hazard;
public Vector3 spawnValues;
public int rainCount;
public float spawnWait;
public float startWait;

void Start ()
	StartCoroutine (SpawnWaves ());

IEnumerator SpawnWaves ()
	yield return new WaitForSeconds (startWait);
	while (true)

		for(int i = 0; i < rainCount; i++)
			Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, 1);
			Quaternion spawnRotation = Quaternion.identity;
			Instantiate (hazard, spawnPosition, spawnRotation);
			yield return new WaitForSeconds (spawnWait);


Here is the problem i’m facing… the script is attached to the cloud… but when you move the cloud… the spawn position stays the same…

That’s because the position given to Instantiate uses world coordinates, not the local coordinates of the object the current script is attached to. Try adding transform.position to the randomly generated spawnPosition:

Instantiate (hazard, spawnPosition + transform.position, spawnRotation);

If the cloud is going to be scaling as well, it may be worth using transform.TransformPoint() to convert spawnPosition from coordinates local to the cloud to world coordinates instead.