Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by davient · Feb 11, 2014 at 07:00 AM · instantiatetransformspriterotateboxcollider

Why does transform.Rotate not move the sprite and BoxCollider2D together?

I want to Instantiate several square prefabs (arrow: BoxCollider2D.isTrigger = true), each rotated at 90 degrees around a central square prefab (engine: Rigidbody2D, BoxCollider2D, Script-see below) of the same size. The squares are each of side length 0.32.

alt text

My principle for achieving this is adding an empty GameObject gob to the engine that I then add the children to with an offset of 0.32, rotating gob after every instantiation of an arrow.

I use the following code inside a script attached to the engine prefab to instantiate the arrows.

     void Start ()
     {
         GameObject gob = new GameObject ();
         gob.transform.position = transform.position;        
         gob.transform.rotation = transform.rotation;
         gob.transform.parent = transform;
         //StartCoroutine("RotFix", gob);
 
         for (int i = 0; i < numAttractors; i++) 
         {
             GameObject att = (GameObject) Instantiate(Resources.Load<GameObject>("arrow"), gob.transform.position, gob.transform.rotation);
             att.transform.parent = gob.transform;
             att.transform.localPosition = new Vector3 (-0.32f,0f,0f);
             gob.transform.Rotate(0f,0f,90);
 
         }
     }

However the result shows me only one arrow: the first arrow, on the left. Turning on the gizmos view in the game window I can see that all the Boxcollider2Ds are where I expected them but the Sprite has not stayed with them:

alt text

Does anyone know why?

rotatebug1.png (12.6 kB)
rotatebug2.png (6.9 kB)
Comment
Add comment · Show 1
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 davient · Feb 11, 2014 at 07:36 AM 0
Share

Using a coroutine to wait 0.5s between each Instantiation we can see that the sprites do rotate into the correct position. But then move on leaving the BoxCollider behind!

 IEnumerator  RotFix1 (GameObject gob)
     {
         for (int i = 0; i < 4; i++) {
             GameObject att = (GameObject) Instantiate(Resources.Load<GameObject>("arrow"), gob.transform.position, gob.transform.rotation);
             att.transform.parent = gob.transform;
             att.transform.localPosition =new Vector3 (-0.32f,0f,0f);
             yield return new WaitForSeconds (0.5f);gob.transform.Rotate(0f,0f,90);
         }
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by davient · Feb 11, 2014 at 08:23 AM

It appears that I had the notion slightly wrong about my transform tree:

My principle for achieving this is adding an empty GameObject gob to the engine that I then add the children to with an offset of 0.32, rotating gob after every instantiation of an arrow.

Should be amended: ...adding an empty GameObject gob to the engine that I then add the children with an offset of 0.32, rotate gob after every instantiation of an arrow, then reparent the correctly positioned arrow to the engine. Then after all are completed Destroy the dummy object gob.

Solution code (replace from line 9-16 in original listing):

         for (int i = 0; i < numAttractors; i++) 
         {
             GameObject att = (GameObject) Instantiate(Resources.Load<GameObject>("arrow"), gob.transform.position, gob.transform.rotation);
             att.transform.parent = gob.transform;
             att.transform.localPosition = new Vector3 (-0.32f,0f,0f);
             att.transform.parent = this.transform;
             gob.transform.Rotate(0f,0f,90);
         }
         Destroy(gob);

Fiddling further shows that by adding a Rigidbody2D the original behavior goes away. That is to say: the sprite and BoxCollider2D move as one.

This appears to be is a Unity bug. I've filed a report: Bug report 590635

Comment
Add comment · Show 1 · Share
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 davient · Feb 18, 2014 at 02:39 AM 0
Share

Bug reported reproducible and sent to development $$anonymous$$m.

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

18 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

Related Questions

How can I make a 2d sprite shoot the direction player is facing? 2 Answers

2D platformer firing left and right, help! 1 Answer

Setting parent of instantiated sprite 2 Answers

How to instantiate on custom rotation? 1 Answer

Instantiate Problem 2 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