Unity学习笔记---控制和交互

接收玩家操作

按键的状态

实体按键:按下---持续---抬起

触摸屏幕:触摸---滑动---抬起

检测按键的方法

//检测键盘按键
Input.GetKeyDown(KeyCode.E)

//按键的3种状态
GetKeyDown();//按下
GetKey();//持续按住
GetKeyUp();//抬起

//鼠标的检测
GetMouseButton();
GetMouseButtonUp();
GetMouseButtonDown();
//鼠标的坐标
Vector3 position = Input.mousePosition
//鼠标的按键
button 取值为 0、1、2,分别表示鼠标左键、右键、中键。

//触摸屏
...

虚拟轴

虚拟轴可以理解为一套按键配置,当符合配置的按键被按下时,虚拟轴的值在-1---1之间变化,来反映按键的力度变化。虚拟轴可以使用在不同平台间的按键适配或者让按键衔接更加自然。

在【Edit→Project Settings→Input】中可以打开 InputManager 配置界面,用户可以在此配置虚拟轴信息。

//虚拟轴的使用方法
Input.GetAxis("");

GetButton();
GetButtonUp();
GetButtonDown();

KeyCode

//按键可以用“a”直接代替
A~Z
F1~F15
Alpha0~Alpha9// 键盘顶部的数字
Left、RightArrow、UpArrow、DownArrow
LeftCtrl、LeftShift、LeftAlt、LeftWindows、RightCtrl、RightShift、RightAlt、RightWindows
Tab、Space、Backspace、Return
Plus加号、Minus减号、Asterisk星号、Slash斜杠、Backslash反斜杠、LeftBracket左括号、RightBracket右括号、Less小于号、Greater大于号、Equals等于号、Caret上尖号、Comma逗号、Period点号、Question问号、Semicolon分号、Colon冒号、Quote单引号、BackQuote反引号、DoubleQuote双引号、Exclaim感叹号、
At @符号、Dollar $符号、Ampersand &符号、Underscore下划线
Insert、Delete、Home、End、PageUp、PageDown、Print
CapsLock、Numlock、ScrollLock
Keypad0~Keypad9、KeypadPeriod
KeypadPlus、KeypadMinus、KeypadMultiply、KeypadDivide
KeypadEquals、KeypadEnter
Mouse0 鼠标左键、Mouse1 鼠标右键、Mouse2 鼠标中间

回调函数

当鼠标和碰撞体组件进行交互时,可以使用回调。需要将脚本挂载在含有碰撞体的物体上。

OnMouseEnter、OnMouseOver、OnMOuseExit//当鼠标进入、悬停、离开碰撞体时调用。

OnMouseDown、OnMouseUp//鼠标按下、抬起时调用

OnMouseUpAsButton()//鼠标按下和抬起在同一个碰撞体时调用。

OnMouseDrag()//鼠标按下并拖拽时调用。

对玩家操作予以反馈

实现方案

移动

        可以使用Transform 组件、Rigidbody 组件、CharacterController 组件对角色控制做出响应。

        Transform 组件:通过改变position也就是游戏物体的位置来实现移动,缺点是遇到有碰撞体检测的情况时,会出现位置抖动的BUG。不便于控制角色爬坡和爬梯。

        Rigidbody 组件通过控制游戏物体的速度,结合物理系统实现移动和控制。缺陷是不便于控制角色爬坡和爬梯,多仅用于2D游戏。

        CharacterController 组件通过控制角色速度和模拟碰撞实现移动。CharacterController 组件能够很方便地实现角色在各种复杂地形中运动。CharacterController 继承 Collider(碰撞体组件Collider)并且其碰撞体是一个胶囊体。CharacterController会与Rigidbody 组件和碰撞体组件存在冲突不能同时使用。

//CharacterController 组件属性
Slope Limit:爬坡最大角度
Step Offset:爬梯最大高度
Skin Width:皮肤厚度
Min Move Distance:最小移动距离
Center、Radius、Height:角色用于检测碰撞的胶囊体中心、半径、高

//CharacterController 组件的使用方法
public CollisionFlags Move(Vector3 motion)
public bool SimpleMove(Vector3 speed)

动作

对话

射线检测
//射线的定义
// origin: 起点, direction: 方向
public Ray(Vector3 origin, Vector3 direction)
// 屏幕射线: 以相机位置为起点, 向近平面上鼠标位置方向投射的射线
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

//发射射线
// ray: 待发射的射线, hitInfo: 碰撞检测信息, maxDistance: 射线发射的最远距离, 返回是否发生碰撞, 只检测第一个碰撞对象
public static bool Raycast(Ray ray)
public static bool Raycast(Ray ray, out RaycastHit hitInfo)
public static bool Raycast(Ray ray, float maxDistance)
public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance)
// 检查射线射程范围内所有碰撞
public static RaycastHit[] RaycastAll(Ray ray)
public static RaycastHit[] RaycastAll(Ray ray, float maxDistance)

//调试射线
// start: 射线起点, dir: 射线方向, color: 射线颜色
public static void DrawRay(Vector3 start, Vector3 dir)
public static void DrawRay(Vector3 start, Vector3 dir, Color color)
// start: 线段起点, end: 线段终点, color: 线段颜色
public static void DrawLine(Vector3 start, Vector3 end)
public static void DrawLine(Vector3 start, Vector3 end, Color color)

//区域碰撞检测
// 检验立方体范围内是否有碰撞体
public static bool CheckBox(Vector3 center, Vector3 halfExtents)
public static bool CheckBox(Vector3 center, Vector3 halfExtents, Quaternion orientation, int layermask)
// 检验球体范围内是否有碰撞体
public static bool CheckSphere(Vector3 position, float radius)
public static bool CheckSphere(Vector3 position, float radius, int layerMask)
// 检验胶囊体范围内是否有碰撞体
public static bool CheckCapsule(Vector3 start, Vector3 end, float radius)
public static bool CheckCapsule(Vector3 start, Vector3 end, float radius, int layermask)

//投射物体
// 投射立方体
public static bool BoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction)
public static bool BoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction, out RaycastHit hitInfo, Quaternion orientation, float maxDistance, int layerMask)
public static RaycastHit[] BoxCastAll(Vector3 center, Vector3 halfExtents, Vector3 direction)
public static RaycastHit[] BoxCastAll(Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation, float maxDistance, int layermask)
// 投射球体
public static bool SphereCast(Ray ray, float radius)
public static bool SphereCast(Ray ray, float radius, out RaycastHit hitInfo, float maxDistance, int layerMask)
public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo)
public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask)
public static RaycastHit[] SphereCastAll(Ray ray, float radius)
public static RaycastHit[] SphereCastAll(Ray ray, float radius, float maxDistance, int layerMask)
public static RaycastHit[] SphereCastAll(Vector3 origin, float radius, Vector3 direction)
public static RaycastHit[] SphereCastAll(Vector3 origin, float radius, Vector3 direction, float maxDistance, int layerMask)
// 投射胶囊体
public static bool CapsuleCast(Vector3 point1, Vector3 point2, float radius, Vector3 direction)
public static bool CapsuleCast(Vector3 point1, Vector3 point2, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask)
public static RaycastHit[] CapsuleCastAll(Vector3 point1, Vector3 point2, float radius, Vector3 direction)
public static RaycastHit[] CapsuleCastAll(Vector3 point1, Vector3 point2, float radius, Vector3 direction, float maxDistance, int layermask)

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值