Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges