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

【Unity】简单案例脚本实现 | 鼠标观察/键盘控制移动飞行/行走/碰撞检测

《Unity5实战-使用C#和Unity开发多平台游戏》第二章-构建一个让你置身3D空间的演示
鼠标观察/键盘控制移动飞行/行走/碰撞检测
Unity版本:2019.4.23f1c1

  • 注意脚本名称和组件添加,不在文章中一一强调
  • 场景模型都是在资源商店选择的免费下载(选择Sort by Price(Low to High)排序)搜索参考关键字:airplane、sky
  • 整篇的实例代码是修改累加的,为了防止混乱我就全部贴上,不强调新增或删减部分

搭建:一架飞机模型(脚本挂载物体),天空盒(照明设置),摄像机Camera
原始画面:
在这里插入图片描述

跟随鼠标观察周围脚本

运行效果:

在这里插入图片描述
如果改为鼠标X则只能水平旋转(我这里是摄像机角度有些倾斜,不是飞机
在这里插入图片描述
在这里插入图片描述

using UnityEngine;
using System.Collections;public class MouseLook : MonoBehaviour
{//观察周围public enum RotationAxes{MouseXAndY=0,MouseX=1,MouseY=2}public RotationAxes axes = RotationAxes.MouseXAndY;public float sensitivityHor = 9.0f;//旋转速度public float sensitivityVert = 9.0f;public float miniumVert = -45.0f;public float maximumVert = 45.0f;private float _rotationX = 0;void Start(){Rigidbody body = GetComponent<Rigidbody>();if (body != null)body.freezeRotation = true;}// Update is called once per framevoid Update(){if (axes == RotationAxes.MouseX)//水平旋转{transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);//GetAxis()获取鼠标的输入}else if (axes == RotationAxes.MouseY)//垂直旋转{_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;//基于鼠标增加垂直角度_rotationX = Mathf.Clamp(_rotationX, miniumVert, maximumVert);//将垂直角度限制在最小值和最大值之间float rotationY = transform.localEulerAngles.y;//保持Y的角度一样(也就是水平没有旋转)transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);//使用存储旋转值创建新的Vector}else//水平且垂直旋转{_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;_rotationX = Mathf.Clamp(_rotationX, miniumVert, maximumVert);float delta = Input.GetAxis("Mouse X") * sensitivityHor;//旋转变化量float rotationY = transform.localEulerAngles.y + delta;//使用delta增加旋转角度transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);}}
}

第一人称控件

在这里插入图片描述

运行效果:键盘WSAD或上下左右,鼠标移动水平高度
在这里插入图片描述在这里插入图片描述
在这里插入图片描述


//using System.Collections.Generic;
using UnityEngine;
using System.Collections;public class FPS : MonoBehaviour
{public float speed = 3.0f;//观察周围public enum RotationAxes{MouseXAndY = 0,MouseX = 1,MouseY = 2}public RotationAxes axes = RotationAxes.MouseXAndY;public float sensitivityHor = 9.0f;//旋转速度public float sensitivityVert = 9.0f;public float miniumVert = -45.0f;public float maximumVert = 45.0f;private float _rotationX = 0;void Start(){Rigidbody body = GetComponent<Rigidbody>();if (body != null)body.freezeRotation = true;}// Update is called once per framevoid Update(){if (axes == RotationAxes.MouseX)//水平旋转{transform.Translate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);//GetAxis()获取鼠标的输入}else if (axes == RotationAxes.MouseY)//垂直旋转{_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;//基于鼠标增加垂直角度_rotationX = Mathf.Clamp(_rotationX, miniumVert, maximumVert);//将垂直角度限制在最小值和最大值之间float rotationY = transform.localEulerAngles.y;//保持Y的角度一样(也就是水平没有旋转)transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);//使用存储旋转值创建新的Vector}else//水平且垂直旋转{_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;_rotationX = Mathf.Clamp(_rotationX, miniumVert, maximumVert);float delta = Input.GetAxis("Mouse X") * sensitivityHor;//旋转变化量float rotationY = transform.localEulerAngles.y + delta;//使用delta增加旋转角度transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);}float deltaX = Input.GetAxis("Horizontal") * speed;float deltaZ = Input.GetAxis("Vertical") * speed;transform.Translate(deltaX, 0, deltaZ);}
}

碰撞检测

问题:如果前面有一堵墙飞机往前会直接穿过去
在这里插入图片描述

运行效果:碰到墙壁无法穿过
在这里插入图片描述

在这里插入图片描述


//using System.Collections.Generic;
using UnityEngine;
using System.Collections;public class FPS : MonoBehaviour
{public float speed = 3.0f;private CharacterController _charController;void Start(){_charController = GetComponent<CharacterController>();}// Update is called once per framevoid Update(){float deltaX = Input.GetAxis ("Horizontal") * speed;float deltaZ = Input.GetAxis ("Vertical") * speed;Vector3 movement = new Vector3 (deltaX, 0, deltaZ);movement = Vector3.ClampMagnitude (movement, speed);movement *= Time.deltaTime;movement = transform.TransformDirection  (movement);_charController.Move (movement);}
}

重力添加

添加重力gravity就能改为陆地行走,在面板将重力改为0则飞行模式
在这里插入图片描述

using System.Collections;
using UnityEngine;[RequireComponent(typeof(CharacterController))]
[AddComponentMenu("Control Script/FPS Input")]
public class FPS : MonoBehaviour
{public float speed = 6.0f;public float gravity = -9.8f;private CharacterController _charController;// Start is called before the first frame updatevoid Start(){_charController = GetComponent<CharacterController>();}// Update is called once per framevoid Update(){float deltaX = Input.GetAxis("Horizontal") * speed;float deltaZ = Input.GetAxis("Vertical") * speed;Vector3 movement = new Vector3(deltaX, 0, deltaZ);movement = Vector3.ClampMagnitude(movement, speed);movement.y = gravity;movement *= Time.deltaTime;movement = transform.TransformDirection(movement);_charController.Move(movement);}
}

相关文章:

  • ZZ308 物联网应用与服务赛题第H套
  • 【Git企业开发】第六节.配置 Git和标签管理
  • 【ES专题】ElasticSearch功能详解与原理剖析
  • 灰度与二值化
  • android studio离线tips
  • 【机试题】编写一个Java函数,实现批量获取数据的功能
  • uniapp 本身就是一个坑,里面还有无数的小坑
  • odoo16前端框架源码阅读——启动、菜单、动作
  • 【Delphi】 各个平台使用 ntfy 效果说明
  • laravel8-rabbitmq消息队列-实时监听跨服务器消息
  • 使用venv 创建虚拟环境
  • 如何安装Node.js? 创建Vue脚手架
  • 【Springboot】基于注解式开发Springboot-Vue3整合Mybatis-plus实现分页查询(二)——前端el-pagination实现
  • 深度学习_11_softmax_图片识别代码原理解析
  • helm 常用命令搜集 —— 筑梦之路
  • 分享的文章《人生如棋》
  • 【css3】浏览器内核及其兼容性
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • Apache Pulsar 2.1 重磅发布
  • EventListener原理
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • JavaScript新鲜事·第5期
  • spring security oauth2 password授权模式
  • TCP拥塞控制
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • 百度地图API标注+时间轴组件
  • 创建一种深思熟虑的文化
  • 前端技术周刊 2019-02-11 Serverless
  • 山寨一个 Promise
  • 小程序开发之路(一)
  • 新手搭建网站的主要流程
  • ​iOS安全加固方法及实现
  • ​一些不规范的GTID使用场景
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • #经典论文 异质山坡的物理模型 2 有效导水率
  • $L^p$ 调和函数恒为零
  • (12)Hive调优——count distinct去重优化
  • (C语言)球球大作战
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (pojstep1.3.1)1017(构造法模拟)
  • (附源码)ssm高校运动会管理系统 毕业设计 020419
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (十五)Flask覆写wsgi_app函数实现自定义中间件
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (转载)hibernate缓存
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .NET Project Open Day(2011.11.13)
  • .NET Standard 支持的 .NET Framework 和 .NET Core
  • .NET 反射的使用
  • .net 验证控件和javaScript的冲突问题
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • @ComponentScan比较
  • [ 渗透测试面试篇 ] 渗透测试面试题大集合(详解)(十)RCE (远程代码/命令执行漏洞)相关面试题
  • []串口通信 零星笔记
  • [16/N]论得趣