unity键盘事件与鼠标事件的简单使用

2 篇文章 0 订阅

键盘事件

按下事件:Input.GetKeyDown()
例如:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shsyhs : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("您按下了“W”");
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            Debug.Log("您按下了“S”");
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("您按下了“D”");
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("您按下了“A”");
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("您按下了“空格”");
        }
    }
}

在这里插入图片描述
抬起事件:Input.GetKeyUp() (用法和按下时间差不多,省略)
长按事件:Input.GetKey()
下面是一个检测连续按下空格次数的例子:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shsyhs : MonoBehaviour
{
    int count = 0;
    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            count++;
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            Debug.Log("您连续按"+count+"次空格");
            count = 0;
        }
    }
}

结果:
在这里插入图片描述
任意键事件
Input.anyKeyDown
Input.anyKey

鼠标事件

按下事件
Input.GetMouseButtonDown()方法:检测鼠标哪个按键被按下,返回参数是0(左键),1(中建),2(右键)
Input.mousePosition(得到鼠标当前坐标)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shsyhs : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("您按下鼠标左键,坐标为"+Input.mousePosition);
        }
        if (Input.GetMouseButtonDown(1))
        {
            Debug.Log("您按下鼠标中键,坐标为" + Input.mousePosition);
        }
        if (Input.GetMouseButtonDown(2))
        {
            Debug.Log("您按下鼠标右键,坐标为" + Input.mousePosition);
        }
    }
}

在这里插入图片描述
抬起事件:Input.GetMouseButtonUp()
对上述代码进行补充:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class shsyhs : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("您按下鼠标左键,坐标为"+Input.mousePosition);
        }
        if (Input.GetMouseButtonDown(1))
        {
            Debug.Log("您按下鼠标中键,坐标为" + Input.mousePosition);
        }
        if (Input.GetMouseButtonDown(2))
        {
            Debug.Log("您按下鼠标右键,坐标为" + Input.mousePosition);
        }
        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("您抬起鼠标左键,坐标为" + Input.mousePosition);
        }
        if (Input.GetMouseButtonUp(1))
        {
            Debug.Log("您抬起鼠标中键,坐标为" + Input.mousePosition);
        }
        if (Input.GetMouseButtonUp(2))
        {
            Debug.Log("您抬起鼠标右键,坐标为" + Input.mousePosition);
        }
    }
}

运行结果:
在这里插入图片描述
注意:上述代码将中键和右键写反了,1是右键,2是中键。抱歉!!自己改一下就好了

长按事件:Input.GetMouseButton()
使用方法和键盘长按事件基本一致,略。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity使用鼠标和键盘来控制相机,您可以编写一个脚本来处理输入,并应用于相机对象。下面是一个示例,演示如何使用鼠标和键盘来控制相机的移动和旋转: ```csharp using UnityEngine; public class CameraController : MonoBehaviour { public float moveSpeed = 10f; public float rotateSpeed = 100f; void Update() { // 移动相机 float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); transform.Translate(new Vector3(horizontal, 0, vertical) * moveSpeed * Time.deltaTime); // 旋转相机 if (Input.GetMouseButton(1)) { float mouseX = Input.GetAxis("Mouse X"); float mouseY = Input.GetAxis("Mouse Y"); transform.Rotate(Vector3.up, mouseX * rotateSpeed * Time.deltaTime); transform.Rotate(Vector3.left, mouseY * rotateSpeed * Time.deltaTime); } } } ``` 在这个示例中,我们定义了`moveSpeed`(移动速度)和`rotateSpeed`(旋转速度)这两个公共变量,可以在Unity编辑器中进行调整。 在`Update`方法中,我们首先获取水平和垂直输入轴的值,根据这些值来移动相机。`Input.GetAxis("Horizontal")`和`Input.GetAxis("Vertical")`可以通过键盘的左右箭头或WASD键来获取输入。 然后,我们使用鼠标右键来旋转相机。当鼠标右键按下时,我们获取鼠标X轴和Y轴的值`Input.GetAxis("Mouse X")`和`Input.GetAxis("Mouse Y")`,并根据这些值来旋转相机。通过调用`transform.Rotate`方法来实现相机的旋转,其中`Vector3.up`表示绕Y轴旋转,`Vector3.left`表示绕X轴旋转。 将此脚本附加到相机对象上,然后您可以使用鼠标右键来旋转相机,使用键盘的方向键或WASD键来移动相机。 请注意,此示例只提供了基本的相机控制功能,您可以根据需要进行扩展和自定义。如果您有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值