Unity关于手游端摇杆移动、摇杆按钮冲突问题
Unity关于手游端摇杆移动、摇杆按钮冲突问题
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- **Unity关于手游端摇杆移动、摇杆按钮冲突问题**
- 一、摇杆移动问题
- 1.问题重现
- 2、解决
- 二、摇杆按钮冲突问题
- 1.问题重现
- 2.解决
- 总结
一、摇杆移动问题
1.问题重现
①摇杆移动时,通过控制角色的刚体运动。
②当摇杆移动到游戏区域外松开时,后面对摇杆进行操作无响应。
③使用的是Drag事件来写的拖动逻辑。
2、解决
解决思路:摇杆控制角色移动时,不能通过控制角色的刚体移动,而要通过改变角色的位置来移动。
(有BUG)刚体移动代码如下(hor、ver为获取了摇杆移动的方向,判断设置了区间-1,1):
direction.x = hor;direction.y = ver * 0.7f;rig.velocity = direction * moveSpeed * 50 * Time.deltaTime;if (rig.velocity.x >= 0.05){transform.rotation = new Quaternion(0, 0, 0, 0);}else if (rig.velocity.x <= -0.05){transform.rotation = new Quaternion(0, 180, 0, 0);}
(可用)使用Translate的移动代码如下:
direction.x = hor;direction.y = ver * 0.7f;player.transform.Translate(direction*moveSpeed*Time.deltaTime);
(可用)最后附上完整代码:
private float moveSpeed;//移动速度private Vector2 direction;//移动方向private PlayerBehaviour behaviour;//角色行为// public Transform Yingzi;//影子public Transform big; //摇杆背景public Transform small;//摇杆中心private float radius = 250;//摇杆半径private Vector2 moveCenter;//移动中心private Vector2 mouseToCenterVect;//鼠标移动到中心的向量private float mouseToCenterDistance;//鼠标到中心点的距离private float hor;//获取水平方向private float ver;//获取垂直方向public Transform player;//主角// Start is called before the first frame updatevoid Start(){behaviour = player.GetComponent<PlayerBehaviour>();}/// <summary>/// 开始拖动时/// </summary>public void Begin(BaseEventData data){//移动中心点赋值moveCenter = big.position;}/// <summary>/// 拖动中/// </summary>/// <param name="_">UI事件数据</param>public void OnDrag(BaseEventData _){//中心点到触摸点的向量赋值mouseToCenterVect = (Vector2)Input.mousePosition - moveCenter;//中心点到触摸点的距离计算mouseToCenterDistance = Mathf.Clamp(mouseToCenterVect.magnitude, 0, 250);//根据距离来判断摇杆中心的位置if (mouseToCenterDistance < radius){//若是距离小于最大半径,这里取向量的归一值,就是模为1的向量,乘上中心到触摸点的距离,这个就是摇杆中心应该移动的方向和距离,并且移动是在移动中心的基础上,所以加上移动中心的坐标small.position = mouseToCenterVect.normalized * mouseToCenterDistance + moveCenter;}else{//同上,不过就是限定了移动的最大距离small.position = mouseToCenterVect.normalized * radius + moveCenter;}//摇杆中心的X - 移动中心的x就是水平的变化值,这里 /100 控制_hor在(-1,,1)之间hor = (small.position.x - moveCenter.x) / 250;//摇杆中心的Y - 移动中心的Y就是垂直的变化值,这里 /100 控制_hor在(-1,,1)之间ver = (small.position.y - moveCenter.y) / 250;}/// <summary>/// 结束时/// </summary>public void End(BaseEventData data){PointerEventData _ = data as PointerEventData;if (_ == null)return;//水平移动值归零hor = 0;//垂直移动值归零ver = 0;small.localPosition = big.localPosition;}void Update(){moveSpeed = behaviour.Speed;direction.x = hor;direction.y = ver * 0.7f; }private void FixedUpdate(){player.transform.Translate(direction*moveSpeed*Time.deltaTime);}
代码详细参考:https://blog.csdn.net/Mr_Sun88/article/details/84680361)
(tips:需要添加Event Trigger组件,并添加Begin Drag、Drag、End Drag)
二、摇杆按钮冲突问题
1.问题重现
导到手机上后:
①摇杆的拖动区域
②左手拖动摇杆时,右手按下按钮,摇杆会往右边移动。
2.解决
解决思路:使用Drag事件所使用到的Input.mousePosition(鼠标位置)不适用于手游的使用,而应该使用多点触控来控制摇杆。
多点触控代码如下:
void Start(){Input.multiTouchEnabled = true;//开启多点触控}void Update(){if (Input.touchCount <= 0) //触摸点数量为0return;if (Input.touchCount >= 1)//触摸点数量不为0{//当触控为左边区域,左边控制虚拟摇杆从而控制player移动if (Input.GetTouch(0).position.x < Screen.width * 0.5f){//只要有左边区域被触摸了,只获取第一次触摸的点if (Input.GetTouch(0).phase == TouchPhase.Began){//移动中心点赋值moveCenter = big.position;}//第一次手指触摸移动if (Input.GetTouch(0).phase == TouchPhase.Moved){//中心点到触摸点的向量赋值mouseToCenterVect = (Vector2)Input.GetTouch(0).position - moveCenter;//中心点到触摸点的距离计算mouseToCenterDistance = Mathf.Clamp(mouseToCenterVect.magnitude, 0, 250);//根据距离来判断摇杆中心的位置if (mouseToCenterDistance < radius){//若是距离小于最大半径,这里取向量的归一值,就是模为1的向量,乘上中心到触摸点的距离,这个就是摇杆中心应该移动的方向和距离,并且移动是在移动中心的基础上,所以加上移动中心的坐标small.position = mouseToCenterVect.normalized * mouseToCenterDistance + moveCenter;}else{//同上,不过就是限定了移动的最大距离small.position = mouseToCenterVect.normalized * radius + moveCenter;}//摇杆中心的X - 移动中心的x就是水平的变化值,这里 /100 控制_hor在(-1,,1)之间hor = (small.position.x - moveCenter.x) / 250;//摇杆中心的Y - 移动中心的Y就是垂直的变化值,这里 /100 控制_hor在(-1,,1)之间ver = (small.position.y - moveCenter.y) / 250;}//第一次手指触摸离开if (Input.GetTouch(0).phase == TouchPhase.Ended){//水平移动值归零hor = 0;//垂直移动值归零ver = 0;small.localPosition = big.localPosition;}}} }
完整代码如下:
private float moveSpeed;//移动速度private Vector2 direction;//移动方向private PlayerBehaviour behaviour;//角色行为// public Transform Yingzi;//影子public Transform big; //摇杆背景public Transform small;//摇杆中心private float radius = 250;//摇杆半径private Vector2 moveCenter;//移动中心private Vector2 mouseToCenterVect;//鼠标移动到中心的向量private float mouseToCenterDistance;//鼠标到中心点的距离private float hor;//获取水平方向private float ver;//获取垂直方向public Transform player;//主角void Start(){behaviour = player.GetComponent<PlayerBehaviour>();Input.multiTouchEnabled = true;}void Update(){moveSpeed = behaviour.Speed;if (Input.touchCount <= 0)return;if (Input.touchCount >= 1){//当触控为左边区域,左边控制虚拟摇杆从而控制player移动if (Input.GetTouch(0).position.x < Screen.width * 0.5f){//只要有左边区域被触摸了,不管Input.touchCount等于多少,都会执行下面if (Input.GetTouch(0).phase == TouchPhase.Began){//移动中心点赋值moveCenter = big.position;}//手指触摸移动if (Input.GetTouch(0).phase == TouchPhase.Moved){//中心点到触摸点的向量赋值mouseToCenterVect = (Vector2)Input.GetTouch(0).position - moveCenter;//中心点到触摸点的距离计算mouseToCenterDistance = Mathf.Clamp(mouseToCenterVect.magnitude, 0, 250);//根据距离来判断摇杆中心的位置if (mouseToCenterDistance < radius){//若是距离小于最大半径,这里取向量的归一值,就是模为1的向量,乘上中心到触摸点的距离,这个就是摇杆中心应该移动的方向和距离,并且移动是在移动中心的基础上,所以加上移动中心的坐标small.position = mouseToCenterVect.normalized * mouseToCenterDistance + moveCenter;}else{//同上,不过就是限定了移动的最大距离small.position = mouseToCenterVect.normalized * radius + moveCenter;}//摇杆中心的X - 移动中心的x就是水平的变化值,这里 /100 控制_hor在(-1,,1)之间hor = (small.position.x - moveCenter.x) / 250;//摇杆中心的Y - 移动中心的Y就是垂直的变化值,这里 /100 控制_hor在(-1,,1)之间ver = (small.position.y - moveCenter.y) / 250;}//手指触摸离开if (Input.GetTouch(0).phase == TouchPhase.Ended){//水平移动值归零hor = 0;//垂直移动值归零ver = 0;small.localPosition = big.localPosition;}}}direction.x = hor;direction.y = ver * 0.7f;}private void FixedUpdate(){player.transform.Translate(direction*moveSpeed*Time.deltaTime);}
代码详细参考:https://blog.csdn.net/qq_39745907/article/details/104010204
tips:这个不需要添加Event Trigger事件。
总结
以上就是本章要讲的内容,本文仅仅讲述了笔者在开发过程中遇到的问题,提供给遇到相同或者相似问问题的开发者参考,欢迎大佬下方评论。
Unity关于手游端摇杆移动、摇杆按钮冲突问题相关推荐
- 梦幻诛仙11职业linux架设手游,一款【梦幻诛仙11职业】手游端私服架设+JAVA后台+架设视频教程...
一款[梦幻诛仙11职业]手游端私服架设+JAVA后台+架设视频教程 安装说明: 最低配置2H4G 安装CentOS 6.8体系 封闭防火墙 chkconfig iptables off service ...
- 手游端游辅助需要掌握那些语言
本章介绍一下做手游外挂需要的了解和掌握的技术并推荐相关的书籍, 方便读者对自己的学习路线做规划. Arm汇编 现在全部ios设备和绝大多数android设备都使用的是arm架构的cpu, 做外挂经常需 ...
- 游戏绑定计算机绑定什么用,lol手游端游绑定有什么用
LOL手机游戏与最终游戏有什么区别? 它适合训练吗? 培训绝对合适,是否没有减少太多,应该与最终游戏没有什么不同,但是应该比最终游客要好很多,现在使用手机玩游戏和计算机都非常方便 太麻烦了 如何绑定手 ...
- Unity 给手游用的高性能辉光(Bloom) Shader
某天项目中美术大哥们提出使用Bloom的需求.但是直接使用Unity 的PostProcess Bloom性能消耗又很大. 所以开发了一个简单的Bloom效果,能满足基本需求,又能兼顾性能.**使用方 ...
- unity3d 手游端一些优化技巧
1.顶点性能 一般来说,如果您想在iPhone 3GS或更新的设备上每帧渲染不超过40,000可见点,那么对于一些配备 MBX GPU的旧设备(比如,原始的 iPhone,如 iPhone 3g和 i ...
- Unity开发手游常用适配分辨率
前言 工作开发中常用的需要适配的手机适配,这里做一下记录. 分辨率 1.新增分辨率 AspectRatio是长宽比,FixedResolution是设置固定分辨率. 2.适配长宽比 平时开发用9:16 ...
- Unity 2D手游——坦克大战 C#
需要下载项目的朋友,请打开CSDN链接:Unity2D坦克大战项目工程 本项目涉及到的功能: 1.UI部分: a .游戏开始界面UI b.主场景UI c.游戏胜利界面UI ...
- IP输出不足,端游手游化时代的单一手游企业如何破局?
在2017年的12月末,一款历经十年沧桑的经典PC端赛车游戏<QQ飞车>又重新回到大众的视野中,并一度夺得IOS免费榜第一的位置. QQ飞车是腾讯专门为QQ用户打造的赛车游戏,伴随不少玩家 ...
- 游戏自评——CF手游
对于移动端FPS游戏--CFm的理解与期待 1.概括性理解 CF手游是一款移动端上的FPS游戏,其IP延续自端游上的CF,可以说天生带着一批粉丝,也可以肯定的是,玩家对于游戏的品质也会有着更高的要求. ...
最新文章
- AI手机报告 | 揭秘手机行业未来AI之路
- 进程的创建-fork
- 在windows上模拟linux环境,MSYS2——Windows平台下模拟linux环境的搭建-Go语言中文社区...
- 数据结构-顺序查找和折半查找
- python程序显示自己的版权_手把手教你Pycharm皮肤主题及个性化设置,python程序员必备-Go语言中文社区...
- 奇怪,有的Python函数或方法调用需要两对括号?
- Learn For Flutter
- Ubuntu 14.04LTS 环境下安装UliPad
- 使用pdfFactory Pro虚拟打印机便笺功能为文件添加批注
- pacman 查询_pacman常用命令
- c语言单片机程序段,51单片机C语言编程基础及实例
- 系列篇|结构光三维重建基本原理
- 电子信息类和计算机类专业网课表
- 区块链基础入门笔记 一
- javase-Calendar
- 【视频制作】PR如何制作追踪马赛克
- 《被讨厌的勇气》- 认真的人生活在当下
- C++----字符集详解(一看就懂系列)
- macOS Big Sur 安装配置QT creator教程
- xp系统怎样访问校园网服务器,有线校园网电脑连接教程