I have an issue that I have been banging my head against for a little bit. Hopefully you amazing people can assist.
I am trying to make it so that when the game starts the camera is in freelook mode. When the player presses LeftControl the camera locks into position and the player can now fly the ship without much worry about losing their view. The ship can pitch and yaw of course. The issue is that when I pitch or yaw and then go into freelook mode the camera always resets the view to 0
The code below is my attempt to fix it. But of course it does not work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Camera CockpitCam;
public GameObject PanelControllerScript;
public bool CamOverRide;
public float ScrollSensitivity = 4;
public float speed = 3;
public float Zoom = 60f;
private Quaternion CamPos;
Vector2 rotation;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
rotation.y = 90;
transform.eulerAngles = (Vector2)rotation;
CockpitCam.enabled = true;
CamOverRide = false;
}
void Update()
{
CamPos = CockpitCam.transform.rotation;
Debug.Log(CamPos);
FreeLookCam();
if (Input.GetKeyDown(KeyCode.LeftControl))
{
if (CamOverRide == false)
{
CamOverRide = true;
}
else if (CamOverRide == true)
{
transform.rotation = CamPos;
CamOverRide = false;
}
}
CockpitCam.enabled = true;
}
private void FreeLookCam()
{
if (CamOverRide == false)
{
if (CockpitCam == true)
{
rotation.y += Input.GetAxis("Mouse X");
rotation.x += -Input.GetAxis("Mouse Y");
transform.eulerAngles = (Vector2)rotation * speed;
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
Zoom = Zoom + ScrollSensitivity;
CockpitCam.fieldOfView = Zoom;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
Zoom = Zoom - ScrollSensitivity;
CockpitCam.fieldOfView = Zoom;
}
if (Input.GetKeyDown(KeyCode.L))
{
CockpitCam.fieldOfView = 60;
rotation.y = 90;
rotation.x = 0;
transform.eulerAngles = (Vector2)rotation;
}
}
}
}
}
Any ideas?