- Home /
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..
Answer by zach-r-d · Jun 21, 2015 at 12:00 PM
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.
Thank you so much for replying so fast!!! It works Pretty well :D and i am using that transform.Transformpoint :D
I'm glad! Remember to check the accept button if it answers your question :)
Your answer
Follow this Question
Related Questions
Phantom Instantiation at Origin 0 Answers
Spawn random enemies in 2D 1 Answer
Instantiated Object spawning at wrong position 3 Answers
What is the best way to spawn enemies? 3 Answers
How to spawn a prefab on a trigger? 3 Answers