I have an issue with getting a gun to rotate around the x axis.
I had a player prefab set up before that was working but due to some networking stuff I had to restructure it. After restructuring the prefab, the gun rotation around the x-axis is broken.
Basically the gun prefab is made up of several objects (the model, grenade spawns and bullet raycasting origin) that appear like so in the following hierarchy:
-gun
-grenade spawn
-model
-raycasting origin
Unfortunately when I rotate the gun around the x axis based on the camera rotation, the grenade spawn and raycasting origin objects both rotate correctly (appearing in the correct position on the screen) however the model seems to be rotating too much.
Here is the code in the gun script that governs the rotation…but I don’t think it’s correct because no matter what i change the rotation to, each component is relative, so if i fix the model rotation, the grenade/bullet raycasting objects rotations will be wrong.
NOTE: THE TARGETYROTATION AS ACTUALLY THE ROTATION AROUND THE X AXIS.
targetXRotation = Mathf.SmoothDamp(targetXRotation, playerView.GetComponent<MouseLook>().rotationX, ref targetXRotationV, rotationSpeed);
targetYRotation = Mathf.SmoothDamp(targetYRotation, -cameraView.GetComponent<MouseLook>().rotationY, ref targetYRotationV, rotationSpeed);
transform.position = cameraView.transform.position + Quaternion.Euler(targetYRotation, targetXRotation, 0) * new Vector3(0,0,currentRecoilZPos);
//Debug.Log ("rotating around the x axis by: " + targetYRotation + " degrees");
Debug.Log ("Cameras rotation around the x axis is: " + cameraView.GetComponent<MouseLook>().rotationY);
transform.rotation = Quaternion.Euler (targetYRotation, targetXRotation, 0);
The below screenshots illustrate what is happening.