- Home /
Instantiated children wont update position
So I decided to make a game for a school project just for fun, it's a typical fly around dodge asteroids 2d game. I have run into one problem though. I have created an empty game object with a square that shows the "spawnable" area for the asteroids. But when I move and the camera/the empty game object updates its position. The spawn area for the asteroids won't, the square will follow the change in position but the asteroids won't.
I have pasted my script that is basically copied from a youtube video and I will also link a youtube video that I recorded to show more in-depth what I mean
Thanks in advance
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class deployDodgebles : MonoBehaviour
{
public GameObject circlePrefab;
public Transform positionSpawner;
public GameObject circles;
public float respawnTime = 1;
private void Start()
{
StartCoroutine("respawnEnemie");
}
private void spawnEnemie()
{
circles = Instantiate(circlePrefab) as GameObject;
circles.transform.parent = positionSpawner.transform;
circles.transform.position = new Vector2(Random.Range(-13.49813f, 13.49813f), 7.347601f);
}
IEnumerator respawnEnemie()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnEnemie();
}
}
private void OnBecameInvisible()
{
Destroy(circles.gameObject);
}
}
Answer by GSGregory · May 06, 2021 at 03:39 PM
That video helped quite a bit. I know its in the early stages but consider naming your circles what they are supposed to be.
Anyway your circles.transform.position = new Vector2(Random.Range() is the problem. Its static. It should be something like (-13f-circles.transform.parent.position, 13f+circles.transform.parent.position) so that the position is relative to the parent position.
Thanks for the quick response, although I'm not sure what you mean? what should go where in the script?
Not sure what you mean by "(-13f-circles.transform.parent.position, 13f+circles.transform.parent.position)" should I replace that instead of random.range?
Sorry had to do something else before I could get back to you. This is what you want.
private void spawnEnemie()
{
circles = Instantiate(circlePrefab) as GameObject;
circles.transform.parent = positionSpawner.transform;
Vector2 parentPosition = new Vector2(circles.transform.parent);
circles.transform.position = new Vector2(Random.Range(-13.49813f+parentPosition.x, 13.49813f+parentPosition.x), 7.347601f);
}
Thanks a lot man, fixed it by using your code and adding the parentsposition on the y axis too so it's following it there too
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class deployDodgebles : MonoBehaviour
{
public GameObject circlePrefab;
public Transform positionSpawner;
public GameObject circles;
public float respawnTime = 1;
private void Start()
{
StartCoroutine("respawnEnemie");
}
private void spawnEnemie()
{
circles = Instantiate(circlePrefab) as GameObject;
circles.transform.parent = positionSpawner.transform;
Transform parentPosition = circles.transform.parent;
circles.transform.position = new Vector2(Random.Range(-13.49813f + parentPosition.transform.position.x, 13.49813f + parentPosition.transform.position.x), 7.347601f + parentPosition.transform.position.y);
}
IEnumerator respawnEnemie()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnEnemie();
}
}
private void OnBecameInvisible()
{
Destroy(circles.gameObject);
}
}
Answer by vargata · May 06, 2021 at 04:53 PM
use Instantiate(Object original, Vector2 position, Quaternion rotation, Transform parent);
where position and rotation will be relative to parent...