- Home /
How to create GameObject (from prefab) at location of BoxCollider2D with rotation?
At the moment I have a rotated BoxCollider2D line (rectangular). It is created from a prefab. At the end of this BoxCollider2D (top right corner / bottom right corner) I want to Instantiate a new rotated BoxCollider2D from the same prefab.
At the moment I create the prefab, and they I try to get the coordinates of both BoxCollider2D's (from the previous line, and the new line). Using this I tried to Instantiate the second line using these positions, but something went wrong. When adding a debug to the locations I found out that they are both the same.
How can I get the correct coordinates to initialize the new line at the correct location? This script is linked to the previous line, which creates a new line when needed (which creates a new line again when needed).
Thanks in advance.
GameObject newLine = Instantiate(line, Vector3.zero, Quaternion.identity);
newLine.transform.eulerAngles = new Vector3(0, 0, Random.Range(-45, 45));
//Variables for this line
BoxCollider2D collider = (BoxCollider2D)this.gameObject.GetComponent<BoxCollider2D>();
float top = collider.offset.y + (collider.size.y / 2f);
float btm = collider.offset.y - (collider.size.y / 2f);
float right = collider.offset.x + (collider.size.x / 2f);
float left = collider.offset.x - (collider.size.x / 2f);
Vector3 topRight = transform.TransformPoint(new Vector3(right, top, 0f));
Vector3 topLeft = transform.TransformPoint(new Vector3(left, top, 0f));
Vector3 btmRight = transform.TransformPoint(new Vector3(right, btm, 0f));
Vector3 btmLeft = transform.TransformPoint(new Vector3(left, btm, 0f));
//Variables new line
BoxCollider2D newLineCollider = (BoxCollider2D)newLine.gameObject.GetComponent<BoxCollider2D>();
float top2 = newLineCollider.offset.y + (newLineCollider.size.y / 2f);
float btm2 = newLineCollider.offset.y - (newLineCollider.size.y / 2f);
float left2 = newLineCollider.offset.x - (newLineCollider.size.x / 2f);
float right2 = newLineCollider.offset.x + (newLineCollider.size.x / 2f);
Vector3 topLeft2 = transform.TransformPoint(new Vector3(left2, top2, 0f));
Vector3 topRight2 = transform.TransformPoint(new Vector3(right2, top2, 0f));
Vector3 btmLeft2 = transform.TransformPoint(new Vector3(left2, btm2, 0f));
Vector3 btmRight2 = transform.TransformPoint(new Vector3(right2, btm2, 0f));
newLine.transform.position = new Vector3(btmRight.x + (btmRight2.x - btmLeft2.x) / 2, btmRight.y, 0);
Debug.Log("btmLeft2x = " + btmLeft2.x + " btmRight2x = " + btmRight2.x);
Debug.Log("btmLeftx = " + btmLeft.x + " btmRightx = " + btmRight.x);
Are you attempting a grid-system that instantiates at runtime? If so, I'd suggest using an offset algorithm like this.
for (int x = 0; x < maxWidth; x++)
{
for (int y = 0; y < maxHeight; y++)
{
Vector2 newPos = new Vector3(x, y);
Instantiate(myPrefab, newPos, Quaternion.Identity);
}
}
I'm creating a game where lines are moving from the right to left. Just before they come into the screen I want to create a new one where the last one left off. I want this to be a continuous line that has different angles. This is why I wanted the coordinates of the top right from the previous line so that I could use those for the positions of the new line. This however didn't work for me.
Your answer
![](https://koobas.hobune.stream/wayback/20220612154345im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiating prefab of object like it was in the scene 0 Answers
BoxCollider2D failling verification when Instantiate (Fixed, Cause: big derp) 1 Answer
Instantiating Objects on the Sides of Other Objects 1 Answer
Script Not Working On Prefab 2 Answers
Instantiated object rotation abruptly. 0 Answers