1
votes

After adding instantiated prefab to parent I want to rotate that new prefab around self Center.

GameObject clone = Instantiate(mockup, transform.position, transform.rotation) as GameObject;

clone.transform.parent = gameObject.transform;
clone.transform.localPosition = Vector3.zero;
clone.transform.Rotate (0, 45, 0);

But it misshape prefab. In case when I not set parent it rotates without misshaping. I tried also RotateAround and pass clone.transform.localePosition as parameter but still the same. I tried also to get center as

clone.GetComponent<Renderer>().bounds.size;

and add to position and that pass as parameter but still no success.

How to rotate object around self center without any misshape ?

Misshape: camera is above and I scale and rotate cube but from above it is not rectangle as it should be

enter image description here

without rotation:

enter image description here

1
What do you mean by misshape?Programmer
@Programmer I added image to question to clarifyMooda
I see nothing wrong with your code. It is rotating the Object you instantiated in local space. Can you show image before rotation and image after rotation. Please take this image from top view with camera face down instead of side view. Maybe that can help determine what's going onProgrammer
@Programmer I added image, when comment this lineclone.transform.parent = gameObject.transform; and rotate it looks good ( I could'n put third image, do not have enough points )Mooda
Is the first image you have top view? It doesn't look like a top view. If not please update the first first picture with image of top view after rotating. Also you said when I not set parent it rotates without misshaping. How do you set the parent? You code does not show you are making it a child of another GameObject? Are you trying to rotate a prefab that is child of another GameObject?Programmer

1 Answers

0
votes

After experiment I was able to replicate your probem. Your problem is on this line of code GameObject clone = Instantiate(mockup, transform.position, transform.rotation) as GameObject;

You are passing transform.rotation as the new position of the new Instantiated GameOBject. transform.rotation is the rotation of the GameObject your script is attached to. The X or the Z axis of that GameObject your script is attached to is are 0. Mine was 25 and yours should be something similar.

Several ways to fix it:

1.Simply make sure that X,Y or Z axis of the GameObject the script is attached to are set to 0. If you don't want to change this then do one of the other solutions below.

enter image description here

2.Another way of solving this is by not proving the position or rotation during Instantiate since you will override both the position and rotation with clone.transform.localPosition = Vector3.zero; and clone.transform.Rotate (0, 45, 0); later on. So, your current code is redundant.

GameObject clone = Instantiate(mockup) as GameObject;

3.Finally, another way to solve this is to provide 0 rotation Quaternion.Euler(Vector3.zero) to the third parameter instead of transform.rotation.

GameObject clone = Instantiate(mockup, transform.position, Quaternion.Euler(Vector3.zero)) as GameObject;