Applying multiple rotations
I have problems with the rotation of a game object. The game is in first person and the player can pick up physics objects from the ground. The object should keep its relative rotation to the camera. The game world is spherical so a rotation also has to be applied to match the player object's rotation when it is moving around the world carrying the object. There is also an ability to rotate the object around the Y axis using the keyboard.
Quaternion playerDeltaRotation = Quaternion.FromToRotation(originalPlayerForward, transform.forward);
closest.transform.rotation = Quaternion.Euler(0, offsetRotation, 0) * originalItemRotation;
closest.transform.rotation = playerDeltaRotation * closest.transform.rotation;
The script is on the player GameObject.
closest is the object having been picked up.
playerDeltaRotation is the rotation that has been applied to the player object by the spherical world because the player has moved since picking up an object.
originalItemRotation is the rotation the object had when it was picked up.
offsetRotation is the Y axis rotation that can be manipulated using the keyboard.
The idea is to first apply the offsetRotation and then the playerDeltaRotation.
Is this a reasonable approach?
This video shows the behaviour
I get strange rotation that sometimes is around the wrong axis. Also, the object seems to make an extra full rotation when passing a certain point of the camera's rotation around the Y axis.
Ins$$anonymous$$d of continuously changing the object rotation, why don't you try setting the character as the parent, that way the rotation of character would pass on to the object and you can then change the rotation of object as a child using the keyboard
Thanks for your answer! I am hesitant to use the parenting approach because I don't want the object's position to follow the player position. I am using physics to keep the object in front of the player so that it works nicely with the physics system.
Can often fix these by changing the order: A*B to B*A. The 1st is using the real axes, while the rest use the current local axis. It can be a real bear to see which is which. You might test by making something a child if you absolutely know it acts like one. If that works, you can duplicate "times my old parent's rotation" in code.
Answer by JonPQ · Aug 28, 2019 at 04:15 PM
Why don't you just parent the object to the camera... the camera is working with no strange rotation... you just need to decide on the offset position, no rotation code required, just parent it to the camera object.