- Home /
Rotating an object taking into account the main camera.
I currently have a script that depending on the joystick axis, rotates the object a certain way, I then send that rotation to the player and orient them on the direction this object is currently facing.
private void Update()
{
h1 = OpenVRController.joystickAxis.x; // set as your inputs
v1 = OpenVRController.joystickAxis.y;
transform.eulerAngles = new Vector3(90f, 0f, Mathf.Atan2(h1, v1) * -180 / Mathf.PI);
rotationAngle = transform.eulerAngles - new Vector3(90, 0, 0);
}
The problem I have is that since I am using eulerangles, if I rotate the camera at all, the way the joystick rotates the object stops making sense in relation to the position of the camera (i.e, If I turn around and press forwards on the joystick, the object points backwards).
I tried using localEulerAngles, and it sorta accomplishes what I want, but since the object is attached to a VR controller, if I rotate the controller, it also rotates the object, which is a no go, since I also need the that the normal of the object I am rotating to stay upwards from the floor.
Is there a way to have localEulerAngles rotate the object while also orienting the object so that it is facing the ground? Or a way to change the script above so that it will take into account camera rotation too?
Thanks in advance!
I'm not sure which axis your camera is rotating on but usually you can add the camera's rotation axis' value to the object you want it to affect. However this will cause the object to constantly turn with the camera so if this is not wanted it should only be done when GetAxis()
doesn't equal zero for the object's movement.
Hey, you were right! I was just overthinking it, turns out that to fix it, all I had to do was add the camera's eulerAngles.y to the rotation of the object, which orients it properly no matter where the camera is pointed.
thanks for adding that comment, and letting other people know how you fixed it. This was exactly the answer I needed for my problem!
Add the rotation of the camera to the rotation of the object/player... you can try adding the euler angles of the camera directly, else work with quaternions and add them, which is probably the best way. transform.rotation = quaternion.Euler whatever angles you want to add.