- Home /
In sequence position game objects
For my 2d game I want to post 10 heart objects in sequence on after other. For this I write following code but it does not work for me. So I want some suggestion in this.
private const float HEART_SIZE = 45f;
public GameObject heartLightPrefab, heartDarkPrefab;
void Start()
{
float startX = 0f,startY=0f;
for (int i = 0; i < Constants.TOTAL_HEARTS; i++)
{
Vector3 heartPosition = new Vector3(startX,startY,0f);
GameObject heartObject = Instantiate(heartLightPrefab, heartPosition, Quaternion.identity) as GameObject;
heartObject.transform.parent = gameObject.transform;
heartPosition = Camera.main.ScreenToWorldPoint(heartPosition);
heartObject.transform.localPosition = heartPosition;
startX += HEART_SIZE;
}
}
Using above code objects start generation from left side of the camera and finishes before it get chance to view in camera.
Also I am trying to position object based on relative to other collection object which is empty game object.
EDIT : At present objects positioned
Have you debugged the heartPositions once they are converted to world coordinates? depending on your resolution and the position of your camera they might not be what you expect.
Yes, I debugged my objects positions. I have edited my question.
If you are going to set position later, why don´t you use the one-parameter version of instantiate
? Also, screenToWorldPoint can be unpredictable sometimes, couldn´t you use regular world coordinates?
Thanks for your answer but you have to give me more information. I will change this thing.
Answer by MrVerdoux · Jan 18, 2014 at 06:36 PM
Place an object at the left side of the screen, with z = -20; another one at the right side also with z = -20. Check their x´s, the difference is the width of your screen in world coordinates at a that certain z. Divide it by 10, and that should be the value of HEART_SIZE.
But as I said in the comments, I would do that in world coordinates directly, which are much easier to handle. That should be something like this:
private const float HEART_SIZE = 45f;
public GameObject heartLightPrefab, heartDarkPrefab;
void Start()
{
float startX = 0f,startY=0f;
for (int i = -Constants.TOTAL_HEARTS/2; i < Constants.TOTAL_HEARTS/2; i++)
{
GameObject heartObject = (GameObject) Instantiate(heartLightPrefab);
heartObject.transform.parent = gameObject.transform;
heartObject.transform.localPosition = new Vector3(i*HEART_SIZE,0,0);
}
gameObject.transform.position = whereverYouWant;
}
I got many things from you answer so thanks for it as well I post my code that work for me.
for (int i = 0; i < Constants.TOTAL_HEARTS; i++)
{
GameObject heartObj = Instantiate(heartLightPrefab) as GameObject;
float objectSizeX = heartObj.renderer.bounds.size.x;
heartObj.transform.parent = gameObject.transform;
heartObj.transform.localPosition =new Vector3(i * objectSizeX * 2f, 0, 0);
}
Your answer
Follow this Question
Related Questions
Object dragging at manual point 2 Answers
GameObjects creation within boundry 3 Answers
At same positioned game objects collision detection 0 Answers
Game Development Approach 0 Answers
Access Prefab Object Via Scirpt 2 Answers