- Home /
X-Flip is making my rotation break?
Hi, I'm trying to make a twin-stick player like in Nuclear Throne, meaning a side-facing sprite with a gun that rotates around him 360 degrees. The graphics x-flip when past 90/-90 degrees, but the x-flip/angle combo isn't working as I'd expect...
The gun's image (on a quad) is a child of my "gun" object, which is the game object I apply the rotation to. In the normal orientation, the "gun" is positioned at the arm-pivot point, and rotation happens around this point just fine.
When flipped, the gun rotates from a weird random pivot (not on the arm). When I look at everything in the scene view, the gun is at the correct arm-pivot point, like it should work. This is the confusing part if I change the angle of the gun object by a tiny amount (0.00001) in the properties window it snaps into the correct position and orientation.
So I don't see any issues with the math, and unity "fixes" it when I edit the angle directly in the properties window - any ideas why the rotation gets messed up in-game?
(note: While looking for an answer, I only found the suggestion to rotate around the Y-axis instead. I'd prefer to not do this for a variety of reasons, including my quads disappearing and my z-ordering getting flipped.)
EDIT: Since you asked, here's some code. It's split between two scripts (one for the character, another for the gun). Here's the character's x-flip code, the facex var is created from the inputs, so you face the way you are moving/shooting. FlipX is a constant with values (-1, 1, 1).
if (facex != 0 && facex > 0 != transform.localScale.x > 0)
transform.localScale = Vector3.Scale(transform.localScale, FlipX);
And here's the code on the weapon which rotates it (and its child with the image quad).
float ang = Mathf.Rad2Deg * Mathf.Atan2(aim.y, aim.x);
if(ang > 90 || ang < -90)
ang = Mathf.Sign(ang) * 180 - ang;
Quaternion q = Quaternion.AngleAxis(ang, Vector3.forward);
transform.rotation = q;
I think the math is correct, because as I pointed out things get 'fixed' when I enter identical values at runtime in the inspector. It's almost like there is some kind of Apply() method I need to call... but that isn't the case right?
Can we see the rotation code for rotating the gun? How are you flipping your player?
try use transform.Rotate(speed*Time.deltaTime,0,0) ins$$anonymous$$d your math.
I'll assume you meant (0,0,speed*Time.deltaTime) to rotate around the Z axis. I already tried this, and it doesn't fix anything.
Answer by callen · Feb 15, 2014 at 05:41 PM
Hopefully someone can come up with a better solution than this, but I managed to fix it using a trick I saw here: http://2dtoolkit.com/forum/index.php?topic=3370.0
Every frame, I can do this on my weapon script and it makes everything work the way it is supposed to work.
gameObject.SetActive(false);
gameObject.SetActive(true);
Of course, this incurs a performance hit averaging around .35ms according to stopwatch. So if I need to animate 12+ networked players using this code, it will probably not be a viable solution. Any real solution would be appreciated!!!
Answer by saulth · Aug 25, 2016 at 03:01 PM
Same problem. Have parent object with x scale = -1. Want to set child positions and rotations, but positioning is not mirrored, and rotations get broken. My work-around is: before updating child rot/pos set the parent scale to 1,1,1 then set child rot/pos, then set parent scale to -1,1,1 which then properly mirrors all children. Using Unity 5.4.