• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jarik1999 · Mar 11, 2018 at 03:09 PM · rotationinstantiateprefabcollider2dtransformpoint

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);

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · Mar 11, 2018 at 03:49 PM 0
Share

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);
     }
 }
avatar image jarik1999 RobAnthem · Mar 11, 2018 at 04:25 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A way to instantiate an item with 90 Degree increments? 2 Answers

Thrown objects always have same rotation 2 Answers

Problem with instantiate rotation 0 Answers

Unity 3.4 Prefab Instance Rotation Issue 0 Answers

2D Projectile Not Firing Based on Rotation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges