- Home /
Spawn point movement problem
Hi. I have problem with my spawn point movement. It should be moving in right direction and spawn "stones". The problem is with spawning, spawn point is moving on X-axis, but it still spawning at the same first position. How to fix it? Please help.
public GameObject Stone;
float randX;
Vector2 whereTospawn;
public float spawnRate = 2f;
float nextSpawn = 1.0f;
//spawn movement
public float speed = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(Vector2.right * Time.deltaTime * speed);
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(20f, 20f);
whereTospawn = new Vector2(randX, transform.position.y);
Instantiate(Stone, whereTospawn, Quaternion.identity);
}
}
Answer by Xype · Sep 07, 2017 at 01:34 AM
you already set up whereTospawn, remove new from whereTospawn = new.
public GameObject Stone;
float randX;
Vector2 whereTospawn;
public float spawnRate = 2f;
float nextSpawn = 1.0f;
//spawn movement
public float speed = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(Vector2.right * Time.deltaTime * speed);
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(20f, 20f);
whereTospawn = Vector2(randX, transform.position.y);
Instantiate(Stone, whereTospawn, Quaternion.identity);
}
}
'Vector2' cannot be used like a method. I cannot remove "new".
Answer by lukasz1675 · Sep 08, 2017 at 02:23 PM
I solve my problem by increasing vector2. Now it is working, not exactly as i want from beginning, but similarly.
public GameObject Stone;
float randX;
Vector2 whereTospawn,vec;
public float spawnRate = 2f;
float nextSpawn = 1.0f;
public float a = 10f, b = 10f;
//spawn movement
public float speed = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(Vector2.right * Time.deltaTime * speed);
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(a, b);
vec = new Vector2(randX, transform.position.y);
whereTospawn = vec;
a += 20;
b += 20;
Instantiate(Stone, whereTospawn, Quaternion.identity);
}
}
Your answer
Follow this Question
Related Questions
Random spawnpoint where only one prefab can spawn! 1 Answer
How would I move enemies at an angle towards the player, instead of just moving them straight? 0 Answers
2D endless scrolling game - Which method is best for moving objects without lags? 2 Answers
Simple Movment 2 Answers
How Do You Make A Character Move On The X and Y Axis and Not Run Thought Walls? 1 Answer