YiHe_AllVersion/YHWeb/Assets/YHElectric/Scripts/InputController/NGMouseLook.cs

106 lines
3.8 KiB
C#

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace YHElectric {
public class NGMouseLook : MonoBehaviour
{
public float sensitivity = 4.0f;
[HideInInspector]
public float sensitivityAmt = 4.0f;//actual sensitivity modified by IronSights Script
private float minimumX = -360f;
private float maximumX = 360f;
private float minimumY = -35f;
private float maximumY = 35f;
[HideInInspector]
public float rotationX = 0.0f;
[HideInInspector]
public float rotationY = 0.0f;
[HideInInspector]
public float inputY = 0.0f;
public float smoothSpeed = 0.35f;
private Quaternion originalRotation;
[HideInInspector]
public float recoilX;//non recovering recoil amount managed by WeaponKick function of WeaponBehavior.cs
[HideInInspector]
public float recoilY;//non recovering recoil amount managed by WeaponKick function of WeaponBehavior.cs
void Start()
{
originalRotation = transform.localRotation;
sensitivityAmt = sensitivity;
}
void Update()
{
if (Time.timeScale > 0 && Time.deltaTime > 0)
{//allow pausing by setting timescale to 0
//Hide the cursor
//Screen.lockCursor = true;
//Screen.showCursor = false;
// Read the mouse input axis
rotationX += Input.GetAxisRaw("Mouse X") * sensitivityAmt * Time.timeScale;//lower sensitivity at slower time settings
rotationY += Input.GetAxisRaw("Mouse Y") * sensitivityAmt * Time.timeScale;
//reset vertical recoilY value if it would exceed maximumY amount
if (maximumY - Input.GetAxisRaw("Mouse Y") * sensitivityAmt * Time.timeScale < recoilY)
{
rotationY += recoilY;
recoilY = 0.0f;
}
//reset horizontal recoilX value if it would exceed maximumX amount
if (maximumX - Input.GetAxisRaw("Mouse X") * sensitivityAmt * Time.timeScale < recoilX)
{
rotationX += recoilX;
recoilX = 0.0f;
}
rotationX = ClampAngle(rotationX, minimumX, maximumX);
rotationY = ClampAngle(rotationY, minimumY - recoilY, maximumY - recoilY);
inputY = rotationY + recoilY;//set public inputY value for use in other scripts
Quaternion xQuaternion = Quaternion.AngleAxis(rotationX + recoilX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationY + recoilY, -Vector3.right);
//smooth the mouse input
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotation * xQuaternion * yQuaternion, smoothSpeed * Time.smoothDeltaTime * 60 / Time.timeScale);
//lock mouselook roll to prevent gun rotating with fast mouse movements
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0.0f);
}
else
{
//Show the cursor
//Screen.lockCursor = false;
//Screen.showCursor = true;
}
}
//function used to limit angles
public static float ClampAngle(float angle, float min, float max)
{
angle = angle % 360;
if ((angle >= -360F) && (angle <= 360F))
{
if (angle < -360F)
{
angle += 360F;
}
if (angle > 360F)
{
angle -= 360F;
}
}
return Mathf.Clamp(angle, min, max);
}
}
}