unity3d 控制鼠标的移动范围

在一些论坛上看到有人问在unity里面控制鼠标的移动范围,有二种方法,一个是调用windows 系统的 user32.dll的ClipCursor函数 

再一种就是 通过Cursor.SetCursor函数

这里就介绍一下 第一种方法吧,不说废话了  直接上代码

 

[DllImport( "user32.dll", CharSet = CharSet.Auto, ExactSpelling = true )]
[return: MarshalAs( UnmanagedType.Bool )]
public static extern bool ClipCursor( ref RECT rcClip );
[DllImport( "user32.dll" )]
[return: MarshalAs( UnmanagedType.Bool )]
public static extern bool GetClipCursor( out RECT rcClip );
[DllImport( "user32.dll" )]
static extern int GetForegroundWindow( );
[DllImport("user32.dll")]
[return: MarshalAs( UnmanagedType.Bool )]
static extern bool GetWindowRect( int hWnd, ref RECT lpRect );
 
[StructLayout( LayoutKind.Sequential )]
public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
    public RECT( int left, int top, int right, int bottom )
    {
        Left = left;
        Top = top;
        Right = right;
        Bottom = bottom;
    }
}
 
RECT currentClippingRect;
RECT originalClippingRect = new RECT( );
 
void Start()
{
    hndl = GetForegroundWindow( );
    GetWindowRect( hndl, ref currentClippingRect );
    GetClipCursor( out originalClippingRect );
    ClipCursor( ref currentClippingRect);
}
 
void OnApplicationQuit()
{
    ClipCursor( ref originalClippingRect );
}
 
 


### 实现Unity3D中角色移动鼠标控制相机旋转 在Unity3D中,实现角色移动鼠标控制相机旋转的功能需要结合输入系统、角色控制器以及摄像机的变换操作。以下是一个完整的解决方案。 #### 1. 鼠标控制相机旋转 使用`Input.GetAxis("Mouse X")`和`Input.GetAxis("Mouse Y")`获取鼠标水平和垂直方向的移动值,并将其应用于摄像机的旋转。 ```csharp public class CameraController : MonoBehaviour { public float sensitivity = 100f; // 灵敏度 public Transform playerBody; // 角色身体 private float xRotation = 0f; void Start() { Cursor.lockState = CursorLockMode.Locked; // 锁定鼠标 } void Update() { float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime; float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime; xRotation -= mouseY; xRotation = Mathf.Clamp(xRotation, -90f, 90f); // 限制上下旋转角度 transform.localRotation = Quaternion.Euler(xRotation, 0, 0); playerBody.Rotate(Vector3.up * mouseX); // 旋转角色身体 } } ``` 此代码实现了通过鼠标控制摄像机的上下左右旋转[^1]。 #### 2. 角色移动 角色移动可以通过键盘输入实现。结合`CharacterController`组件,可以更方便地处理角色的移动逻辑。 ```csharp public class PlayerMovement : MonoBehaviour { public CharacterController controller; public float speed = 12f; public float gravity = -9.81f; public float jumpHeight = 3f; private Vector3 velocity; private bool isGrounded; void Update() { isGrounded = controller.isGrounded; if (isGrounded && velocity.y < 0) { velocity.y = -2f; // 调整下落速度 } float moveX = Input.GetAxis("Horizontal"); float moveZ = Input.GetAxis("Vertical"); Vector3 move = transform.right * moveX + transform.forward * moveZ; controller.Move(move.normalized * speed * Time.deltaTime); if (Input.GetButtonDown("Jump") && isGrounded) { velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); } velocity.y += gravity * Time.deltaTime; controller.Move(velocity * Time.deltaTime); } } ``` 此代码实现了基于键盘输入的角色移动功能,包括前后左右移动和跳跃。 #### 3. 组合功能 将上述两个脚本分别附加到摄像机和角色对象上即可实现完整功能。确保摄像机作为角色的子对象,以便角色移动时摄像机跟随。 --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值