- Home /
Rotate 3D object relative to the camera
Hello, I really need the help of an expert. I'm trying to rotate an object in all 3 axes relative to the camera in response to the gamepad sticks. I have succeeded but with an unexpected error, depending on its orientation, the object can rotate in the opposite direction of the stick direction.
For example, if the object is on its back, when I move the stick to the left the object rotates to the left. But, when the object is facing the camera, when I move the stick to the left the object rotates to the right. The same happens in all 3 axes, on each axis I have 180 degrees with the correct stick orientation and 180 degrees with inverted sticks. Also, both the camera and the object can freely move adding more complexity to this problem.
I would like to find a way where the orientation of the rotation always follows the direction of the stick. This is solved with a minus sign applied to the angle according to the object orientation (relative to the camera) but I can't find out how.
This is the code I'm using, it works fine except for the orientation problem. Can anyone find the solution to this problem?
public void ObjectRotate(int dir, float angle) // Object is defined elsewhere
{
var rotate = 360 * Time.deltaTime * angle;
var target = Object.transform;
if(dir == 1) { target.RotateAround(target.transform.position, Camera.main.transform.right, rotate ); }
else if(dir == 2) { target.RotateAround(target.transform.position, Camera.main.transform.up, rotate); }
else if(dir == 3) { target.RotateAround(target.transform.position, Camera.main.transform.forward, rotate); }
}
Your answer
Follow this Question
Related Questions
Rotate object with camera 0 Answers
Clamp RotateAround is not working on Camera. 1 Answer
Programmatically Find Pivot Point 0 Answers