- Home /
Flipping parent object in 2d with negative x scale causes rotation issues for children gameobjects
When I flip my my parent game object from positive x to -x image size to mirror and make it appear as if the image went from moving left to right, the children game objects no longer rotate properly when using a rotation script. It appears that it isn't just rotating improperly but rotating in a weird way altogether. I'm using Unity 4.5.1f3, I know there was a problem with 2d collider rotation but this seems to be outside of that because the entire game object is rotating abnormally.
Does anyone know how to overcome this problem?
Script for arm rotation, arms are attached to parent game object. When the parent flips size from positive to negative, the image mirrors and all children flip as well, but when rotating, problems occur:
void Update ()
{
target = this.transform;
mouse_pos = Input.mousePosition;
mouse_pos.z = 0f; //The distance between the camera and object
object_pos = gameManager.mainCamera.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x *-1.0f - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis( angle+105.0f, Vector3.forward);
}
Exact same problem, has anybody come up with a solution for this?
Answer by IvovdMarel · Jun 27, 2014 at 06:47 PM
Try changing transform.rotation to transform.localRotation
First thing I tried, but for some odd reason it does nothing :(
Answer by Pyrian · Nov 14, 2014 at 04:47 PM
The Atan2 call is not aware of the flip, so it returns an unflipped result. You pass that unflipped result into the rotation value, which is then modified by the scale to flip it. This of course results in a value that's flipped from what you want it to point at.
You'll have to manually flip the angle you're passing while the sprite is flipped.