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;
           
        }
    }

   
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山竹炒大蒜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值