本文主要是介绍unity 鼠标锁住不动旋转摄像头视角,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
把原来光标隐藏 找一个图片放在中间移动完毕真实鼠标光标显示出来
//******************************************************************
//
// 文件名(File Name): ViewControl.cs
//
// 功能描述(Description): 光标不动旋转摄像机
//
// 作者(Author): sqw
//
// 日期(Create Date): 2019.10.10
//
//******************************************************************using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class ViewControl:MonoBehaviour
{public static extern int SetCursorPos(int x, int y);enum RotationAxes{MouseXAndY,MouseX,MouseY}RotationAxes axes = RotationAxes.MouseXAndY;float sensitivityX = 10;float sensitivityY = 10;float minimumY = -60;float maximumY = 60;private float rotationY = 0;public Image cursor;private void Start(){}public void Update(){if (Input.GetMouseButton(0)){Cursor.lockState = CursorLockMode.Locked;//锁定指针到视图中心Cursor.visible = false;//隐藏指针Cursor.visible = false;cursor.enabled = true;if (axes == RotationAxes.MouseXAndY){float rotationX = Camera.main.transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;rotationY += Input.GetAxis("Mouse Y") * sensitivityY;rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);Camera.main.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);}else if (axes == RotationAxes.MouseX){Camera.main.transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);}else{rotationY += Input.GetAxis("Mouse Y") * sensitivityY;rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);Camera.main.transform.localEulerAngles = new Vector3(-rotationY, Camera.main.transform.localEulerAngles.y, 0);}}if (Input.GetMouseButtonUp(0)){Cursor.visible = true;//显示指针Cursor.lockState = CursorLockMode.None;//锁定指针到视图中心cursor.enabled = false;}}}
这篇关于unity 鼠标锁住不动旋转摄像头视角的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!