当前位置: 首页 > news >正文

Input输入和Screen相关

知识点

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Lesson11 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){#region 注意,输入内容一定写在Update中#endregion#region 知识点一 鼠标在屏幕的位置//print(Input.mousePosition);//鼠标原点在屏幕的左下角,且x向右,z轴为0#endregion#region 知识点二 检测鼠标输入//0是左键1是右键2是中键if(Input.GetMouseButtonDown (0)){print("鼠标左键按下了");}if((Input .GetMouseButtonUp (0))){print("鼠标左键抬起了");}if(Input .GetMouseButton (1)){print("右键按下了");}//获取鼠标滚轮值//返回值是一个vector2向量,鼠标中建滚懂,会改变 Y值//-1是往下滚,1是往上滚print(Input.mouseScrollDelta);#endregion#region 知识点三 键盘检测//键盘按下if(Input.GetKeyDown(KeyCode.W)){print("W键按下");}//键盘抬起Input.GetKeyUp(KeyCode.W);//键盘长安Input.GetKey (KeyCode.W);#endregion#region 知识点四 检测默认轴输入//我们学习鼠标 键盘输入 主要是用来//控制玩家 比如 旋转 位移等等//所以Unity提供了 更方便的方法 来帮助我们控制 对象的 位移和旋转//键盘AD按下时 返回 -1到1之间的变换//相当于 得到得这个值 就是我们的 左右方向 我们可以通过它来控制 对象左右移动 或者左右旋转//print(Input.GetAxis("Horizontal"));//键盘sw按下时 返回 -1到1之间的变换//得到得这个值 就是我们的 上下方向 我们可以通过它来控制 对象上下移动 或者上下旋转print(Input.GetAxis("Vertical"));//鼠标横向移动时 -1 到 1 左 右print(Input.GetAxis("Mouse X"));//鼠标竖向移动时  -1 到 1 下 上print(Input.GetAxis("Mouse Y"));//GetAxisRaw方法 和 GetAxis使用方式相同//只不过 它的返回值 只会是 -1 0 1 不会有中间值#endregion}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Lesson12 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){#region 知识点一 静态属性#region 常用//当前屏幕分辨率Resolution r= Screen.currentResolution;print("当前屏幕分辨率的宽" + r.width + "高" + r.height);//屏幕窗口当前宽高print(Screen.width);print(Screen.height);//这得到的是当前窗口的宽高 不是设备分辨率的宽高//一般写代码 要 用窗口宽高 做计算是就用他们//屏幕休眠模式Screen.sleepTimeout = SleepTimeout.NeverSleep;#endregion#region 不常用//运行时是否全屏模式Screen.fullScreen = true;//窗口模式//独占全屏FullScreenMode.ExclusiveFullScreen//全屏窗口FullScreenMode.FullScreenWindow//最大化窗口FullScreenMode.MaximizedWindow//窗口模式FullScreenMode.WindowedScreen.fullScreenMode = FullScreenMode.Windowed;//移动设备屏幕转向相关//允许自动旋转为左横向 Home键在左//允许自动旋转为右横向 Home键在右//允许自动旋转到纵向 Home键在下#endregion#region 知识点二 静态方法//设置分辨率 一般移动设备不使用Screen.SetResolution(1920, 1080, true);#endregion #endregion}// Update is called once per framevoid Update(){}
}

练习题

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TankMove : MonoBehaviour
{public int moveSpeed = 10;public int rotateSpeed = 50;public int headRetateSpeed = 50;public int cremaRetateSpeed = 30;public Transform head;public Transform turret;public Transform crema;// Start is called before the first frame updatevoid Start(){//写一个方法,控制坦克的移动,炮台的转向}// Update is called once per framevoid Update(){Move();}public void Move(){this.transform.Translate(Vector3.forward * moveSpeed  * Time.deltaTime * Input.GetAxis("Vertical"),Space.World );this.transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime * Input.GetAxis("Horizontal"));//坦克头的转向head.Rotate(Vector3.up * headRetateSpeed  * Time.deltaTime * Input.GetAxis("Mouse X"));//鼠标中键控制坦克炮管抬起放下turret.Rotate(Vector3.forward * headRetateSpeed * Time.deltaTime * Input.mouseScrollDelta.y );//控制摄像头看向坦克,按下鼠标左键移动鼠标可以观测坦克if(Input .GetMouseButton (1)){crema.RotateAround(this.transform.position, Vector3.up, cremaRetateSpeed * Time.deltaTime*Input .GetAxis ("Mouse X"));}}
}

http://www.dtcms.com/a/288529.html

相关文章:

  • 一键创建netcore8.0项目
  • 电机相关常见名词
  • 国产电科金仓数据库金仓KES V9 2025:AI时代的数据库融合标杆
  • Temperature 是在LLM中的每一层发挥作用,还是最后一层? LLM中的 Temperature 参数 是怎么计算的
  • 信息学奥赛一本通 1579:【例 5】皇宫看守 | 洛谷 P2458 [SDOI2006] 保安站岗
  • 明细列表,明细grid中的默认按钮失效,配置按钮失效
  • Solidity 中的`bytes`
  • [AI风堇]基于ChatGPT3.5+科大讯飞录音转文字API+GPT-SOVITS的模拟情感实时语音对话项目
  • min-max容斥学习笔记
  • 线性回归问题
  • MC0463四大名著-水浒签到
  • 智慧酒店房价牌系统,国际版,海外版 的设计与应用价值研究———仙盟创梦IDE
  • NX二次开发常用函数——从一个坐标系到另一个坐标系的转换(UF_MTX4_csys_to_csys )相同体坐标转化
  • 文献精读:青藏高原东北部青海湖流域沿海拔分布的蒸散量及其主要影响因素
  • 问津集 #1:Rethinking The Compaction Policies in LSM-trees
  • 计算机组成原理20250719——计算机系统概述
  • 创建套接字并bind的详细过程
  • 图片放大镜案例
  • 关于Spring RestTemplate
  • Java多线程进阶
  • 【计算机网络架构】树型架构简介
  • openmv循迹
  • 基于Scrapy-Redis的分布式爬虫系统:工业级实现与深度优化
  • Linux系统日志管理入门:journalctl命令完全指南
  • Python关于numpy的基础知识
  • 物理AI是什么技术?
  • LVS实验步骤解析
  • yolo8实时识别目标(和平精英敌人+骨骼关键点)
  • 云计算与 DevOps(开发与运维)
  • 分立元件线性稳压器12V转5VMultisim仿真