yRotation = transform.rotation.y;
transform.rotation = Quaternion.Euler(0,yRotation,0);
This locks rotation on all axes for some reason, including Y.
I need to be able to lock and unlock the rotation though. Here is the full script:
using UnityEngine;
using System.Collections;
public class playerMovement : MonoBehaviour
{
float horizontalAxis;
float verticalAxis;
float deltaTime;
int speed;
Quaternion rotation;
int mouseSensitivity;
int pitchSpeed;
public string moveMode;
int gravityFactor;
int jumpHeight;
CharacterController controller;
Vector3 moveVector;
public float yRotation;
void Start()
{
speed = 1000;
mouseSensitivity = 300;
Screen.showCursor = false;
pitchSpeed = 30;
moveMode = "flight";
gravityFactor = 30;
jumpHeight = 30;
controller = GetComponent<CharacterController>();
}
void Update()
{
if (moveMode == "flight")
{
deltaTime = Time.deltaTime;
horizontalAxis = Input.GetAxis("Horizontal");
verticalAxis = Input.GetAxis("Vertical");
if (verticalAxis != 0)
rigidbody.AddRelativeForce(0,0,verticalAxis * deltaTime * speed);
if (horizontalAxis != 0)
transform.Rotate(0,0,-horizontalAxis * deltaTime * pitchSpeed);
if (Input.GetKey(KeyCode.Space))
rigidbody.AddRelativeForce(0,1 * speed * deltaTime,0);
if (Input.GetKey(KeyCode.LeftShift))
rigidbody.AddRelativeForce(0,-1 * speed * deltaTime,0);
if (Input.GetAxis("Mouse Y") != 0)
transform.Rotate(-Input.GetAxis("Mouse Y") * deltaTime * mouseSensitivity,0,0);
if (Input.GetAxis("Mouse X") != 0)
transform.Rotate(0,Input.GetAxis("Mouse X") * deltaTime * mouseSensitivity,0);
rigidbody.angularVelocity = new Vector3(0,0,0);
}
else
{
yRotation = transform.rotation.y;
transform.rotation = Quaternion.Euler(0,yRotation,0);
}
}
}