- Home /
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.
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:
Does anyone know why?
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);
}
}
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
Bug reported reproducible and sent to development $$anonymous$$m.
Your answer
Follow this Question
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
How do I make gameObject.transform.rotation.z equal to a set float value? 2 Answers