- Home /
The question is answered, right answer was accepted
How to control mouse look of standard First Person Controller? [Unity 5.x]
Hey there!
I'm using the FPSController.prefab Standard Asset from the Characters package, and I would like to modify the rotation of my FPSController. However, the MouseLook object in FPSController stores its own target rotation, so it will overwrite my desired rotation.
For example, I the below TunrPlayer() function doesn't make sense, because MouseLook.LookRotation() will overwrite the player rotation:
public void TurnPlayer(FirstPersonController player, float angle) {
player.transform.rotation *= Quaternion.Euler(0, angle, 0);
}
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);
// skipping some irrelevant lines...
character.localRotation = m_CharacterTargetRot;
camera.localRotation = m_CameraTargetRot;
// skipping some more irrelevant lines...
}
I tried writing extending the FirstPersonController script with the below method, but it does not solve my problem:
public void ResetMouseLook() {
m_MouseLook.Init(transform, m_Camera.transform);
}
// this MouseLook.Init() exists in MouseLook.cs by default
public void Init(Transform character, Transform camera)
{
m_CharacterTargetRot = character.localRotation;
m_CameraTargetRot = camera.localRotation;
}
What I'm trying to achieve is that if the FirstPersonController's transform is rotated the MouseLook script should respect it, and rotate with it. What am I missing here? How could I gain better control over the MouseLook?
Thanks, Harinezumi
Answer by spood · Jul 10, 2016 at 09:43 PM
I ran into this exact same issue when I added cutscenes. My goal was to use the FPS controller as the camera for the cutscene, and then give control back to the player.
You have the right idea to update the m_CharacterTargetRot and m_CameraTargetRot inside MouseLook. When the FirstPersonController calls LookRotation() inside MouseLook, it will use these updated values.
There's a second part that I was getting stuck on. Changing the rotations of them to the new transforms ends up on a tilted axis if you don't lock the X and Z axis on both the camera and character. Although from what I understand the camera should have a locked Y and Z axis, but this wasn't working for me, only the X and Z axis locking worked, not sure why.
Anyway, this way worked for me. Hope this helps anyone else looking into it:
Inside FirstPersonController:
private void Update ()
{
if(cutsceneManager.isInCutscene) {
RotateViewCutscene();
return;
}
RotateView ();
// other FPS controller code
}
private void RotateViewCutscene()
{
m_MouseLook.LookRotationCutscene (transform, m_Camera.transform);
}
private void RotateView ()
{
m_MouseLook.LookRotation (transform, m_Camera.transform);
}
Inside MouseLook:
public void LookRotationCutscene(Transform character, Transform camera)
{
m_CharacterTargetRot = character.localRotation;
m_CameraTargetRot = camera.localRotation;
}
And for completions sake, this is when I do the rotation inside my cutscene:
Quaternion targetRotation = Quaternion.LookRotation(targetPosition - thing.transform.position);
// lock x and z axis
targetRotation.x = 0.0f;
targetRotation.z = 0.0f;
float t = runningTime / Duration;
thing.transform.rotation = Quaternion.Slerp(initialRotation, targetRotation, t);
Yes, this approach (although a bit specific) will work. I think I did something similar, or turned off the FirstPersonController when I wanted direct control.