- Home /
Change FirstPersonController to use global rotation values
Hey all. I'm making a game using a slightly modified version of the RigidbodyFirstPersonController from the standard assets. I have a visual effect which involves rotating the camera on the Z axis to simulate a "bullet dodging" sort of action.
The trouble is that the LookRotation function (in MouseLook.cs) uses the camera's Z orientation when it rotates the camera. I.E., if you pull the mouse down while the camera is tilted, it will rotate the camera "downward" relative to the camera's rotation, i.e. not straight down, as opposed to rotating it toward the global "downward".
Because of this, trying to aim while the dodge effect is active has unpredictable results, since the crosshair isn't moving the way it normally would. And since the effect is very quick, and a lot of the game relies on fast, precise aiming, I would rather have the camera rotation be around the global Y axis, rather than the local Y axis. I'm just not sure how to change the current code to make that work.
Here's the code as it is, for reference:
public void LookRotation(Transform character, Transform camera)
{
float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
m_CharacterTargetRot *= Quaternion.Euler (0f, yRot, 0f);
m_CameraTargetRot *= Quaternion.Euler (-xRot, 0f, 0f);
//THIS FIXES DODGING ISSUES
m_CameraTargetRot = Quaternion.Euler(m_CameraTargetRot.eulerAngles.x, m_CameraTargetRot.eulerAngles.y, camera.localEulerAngles.z);
if(clampVerticalRotation)
m_CameraTargetRot = ClampRotationAroundXAxis (m_CameraTargetRot);
if(smooth)
{
character.localRotation = Quaternion.Slerp (character.localRotation, m_CharacterTargetRot,
smoothTime * Time.deltaTime);
camera.localRotation = Quaternion.Slerp (camera.localRotation, m_CameraTargetRot,
smoothTime * Time.deltaTime);
}
else
{
character.localRotation = m_CharacterTargetRot;
camera.localRotation = m_CameraTargetRot;
}
UpdateCursorLock();
}
The "this fixes dodging issues" line was what I needed to add so that the LookRotation function didn't override the Z-axis rotating my other script was doing.
Thanks for the help.
Your answer
![](https://koobas.hobune.stream/wayback/20220612170618im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to use quaternions to change the rotation of a object when you have a joint linking 2 objects? 0 Answers
using difference in 2 quaternions to cancel out starting position of gyro 0 Answers
Need help with storing Rotation in a Quaternion 1 Answer
First Unity tryOut - First Person Controller makes all objects just Float away... 3 Answers
Move Character Controller along Box 0 Answers