public class MouseLook : MonoBehaviour
{
private Camera mCamera;
public float mouseXSensitivity = 200f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
mCamera = GetComponent();
}
// Update is called once per frame
void Update()
{
//鼠标滚轮的效果
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
//Debug.Log(123);
if (mCamera.fieldOfView <= 100)
{
mCamera.fieldOfView += 2;
}
if (mCamera.orthographicSize <= 20)
{
mCamera.orthographicSize += 0.5F;
}
}
//Zoom in
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
//Debug.Log(456);
if (mCamera.fieldOfView > 2)
{
mCamera.fieldOfView -= 2;
}
if (mCamera.orthographicSize >= 1)
{
mCamera.orthographicSize -= 0.5F;
}
}
if (Input.GetMouseButtonDown(1))
{
//Cursor.visible = false;
//Cursor.lockState = CursorLockMode.Confined;
//Cursor.lockState = CursorLockMode.Locked;
}
// Unlock and show cursor when right mouse button released
if (Input.GetMouseButtonUp(1))
{
//Cursor.visible = true;
//Cursor.lockState = CursorLockMode.Confined;
//Cursor.lockState = CursorLockMode.None;
}
if (Input.GetMouseButton(1))
{
float mouseX = Input.GetAxis("Mouse X") * mouseXSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseXSensitivity * Time.deltaTime;
if (mouseX > 10 || mouseX < -10 || mouseY > 10 || mouseY < -10)
{
return;
}
if (mouseX > -0.1 && mouseX < -0.1)
{
return;
}
if (mouseY > -0.1 && mouseY < -0.1)
{
return;
}
if (mouseX != 0 || mouseY != 0)
{
//print("mouseX:" + mouseX + " && mouseY:" + mouseY);
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
}
}