- Home /
How to flip objects that are currently rotating?
In this gif, what I want is for the arms and bow to flip when the player is flipped, so it doesn't look like they are upside down.
I have tried making the arms a child of a parent, and then flipping the parent's x and y, but it didn't seem to do anything, except for rotating the arms when the main rotating has stopped.
This the code (in the Bow object) i am using for rotating the arms and bow:
// Rotate Bow
float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
pivot.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
RotatePlayer();
// Rotate Arm
if (playerBowHoldShoulder != null) {
playerBowHoldShoulder.rotation = Quaternion.AngleAxis(angle - 5.78f, Vector3.forward);
playerBowDrawShoulder.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
The RotatePlayer() method just flips the player's Y rotation when the bow's xPos is greater than the player's xPos, and vice versa, which works fine as you can tell by the first gif.
This below is the hierarchy view of the Player object Where the highlighted areas are the one's affected by the rotation you see in the gifs.
Any help or ideas would be appreciated, thanks! Let me know if I need to provide more info.
@awesomealvin Idea: You can get Vector3.forward for player hands. Then check if its x value is positive or negative (looking right or left). In one of the cases switch their rotation in the same way as player objects.
Axis you need to check/flip may be different in your project :) Need further help? :)
Yeah that's what I tried to the arms itself, but the rotation script keeps overriding the script that's trying to flip it. So that's why I used shoulders_pivot, but trying to flip that only affects it after the main rotation is not being used.
Answer by awesomealvin · Jan 11, 2018 at 09:27 AM
So I have kind of fixed it except I ran into another issue. But this pretty much resolves my initial problem:
First I also made the bow_pivot a child of the shoulders_pivot.
Then in the RotatePlayer(), it calls a function in the player object that flips the shoulders pivot.
Here's the code for that:
bool RotatePlayer() {
if (player != null && player.isDrawing) {
float difference = transform.position.x - player.transform.position.x;
if (difference >= 0) {
player.LookRight();
return true;
} else {
player.LookLeft();
return false;
}
}
return true;
}
Returns true when facing right.
And here's the code for the LookLeft and LookRight:
public void LookLeft() {
Look(180f);
}
public void LookRight() {
Look(0.0f);
}
private void Look(float angle) {
transform.rotation = Quaternion.AngleAxis(angle, Vector3.up);
// To flip the arm and bow
if (isDrawing) {
shouldersPivot.eulerAngles = new Vector3(angle, 0f, 0f);
}
}
Now with this and the original rotation code, the rotation won't flip. So changing the .rotation to .localRotation makes it work. I don't know why but it just does. So the arms and bow will flip, but when facing the left side, the rotation becomes inverted. So I changed the code a bit to solve that:
// Rotate Bow
float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
pivot.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
bool isLookingRight = RotatePlayer();
angle = (isLookingRight) ? angle : -angle;
pivot.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
// Rotate Arm
if (playerBowHoldShoulder != null) {
playerBowHoldShoulder.localRotation = Quaternion.AngleAxis(angle - 5.78f, Vector3.forward);
playerBowDrawShoulder.localRotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
And now the flip works correctly. So I guess I can resolve this question. But the new problem that you can see in the gif is that when shooting towards the left side, the rotation will look at the right side for one frame. I have been struggling with this issue for a couple hours and no idea how to fix it. Note that in the gif, only the first shot seems to have this issue, every other left side shot does. The gif just didn't capture the exact frame that the bug happens.
I don't know much about rotations, so sadly I can't help you 100%
But I would intuitively say that this problem isn't just with the left side. You just don't see it on the right side because the rotation is already right?
Ah, yeah that could be true, thanks! That should help me find the problem. I'll post a reply to the solution if I solve it.