JoyStick 之前一直不知道发布到手机上怎么操作,看到网上有人说,桌面端上的鼠标点击和手机上的触控点击是一样的,把电脑上的WSAD键改成四个button就行,鼠标左键攻击也改成手机上的一个button,其他一些功能需要专门学习移动端的触控Touch方法,当时一想,改成button就很简单,Touch还是算了,懒得想,觉得很难。(不想用插件)
后面实际操作时,又想到玩手机游戏都是左边有摇杆,想想还是摇杆更好,就搜了大佬的教程,怎么判断移动呢,之前桌面端就利用 float x = Input.GetAxis(“Horizontal”); float z= Input.GetAxis(“Vertical”);现在手机上用摇杆,就用摇杆在X和Z方向的移动值传给float x,float z即可;
然后令我困扰一天的事情发生了,在使用左边摇杆控制人物移动,左边我点击攻击按钮就行攻击时,触控发生了冲突,我只要点击攻击按钮,左边的摇杆就变成朝我右手点击的按钮方向了,我很困扰为什么不能同时左边点击虚拟摇杆控制人物移动,右边点击按钮控制人物攻击,我用关键字搜索很多结果,也有我这样的问题,但没有明确的回答(现在想想知道了,因为太简单了。。emm),其中我看到一个关于双摇杆的实现,我觉得有点类似,都是两边触控,然后研究了一会,额,复杂,我不怎么看得懂,头疼,我只是想两边都能正常点击实现相应功能,两边同时点击的时候不冲突就行了;然后又硬着头皮搜了Touch相关的资料,实在没办法,不想学也要学习了,最后还是在双摇杆实现那个代码中,我看了几遍,突然想到,,不对,我这边是因为没有开启多点触控!!!这下子恍然大悟,懂了,然后我还想跟桌面端那样鼠标滑轮滚动控制视角的缩放,最后结合大佬的代码,我把屏幕分成三部分,左边虚拟摇杆的显示和控制移动,中间双指缩放控制视角,右边是按钮的点击;
代码如下:
(1) player移动控制:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class EasyTouchMove : MonoBehaviour{
//摇杆移动最大半径
public float maxRadius = 100;
//初始化背景图标位置
public Transform moveBackPos;//虚拟摇杆背景位置(摇杆底盘)
public Transform joy; //虚拟摇杆位置
private Vector2 moveCenter;
//hor,ver的属性访问器
private float horizontal=0;
private float vertical=0;
public float Horizontal {
get { return horizontal; }
}
public float Vertical {
get { return vertical; }
}
void Start(){
moveCenter = joy.position;
Input.multiTouchEnabled = true;//开启多点触控
}
void Update () {
horizontal = (joy.position.x - moveCenter.x)/100;
vertical = (joy.position.y - moveCenter.y)/100;
if (Input.touchCount <= 0)
return;
if (Input.touchCount >= 1)
{
//记录每个手指的触控信息
for (int i = 0; i < Input.touchCount; i++)
{
//当触控为左边区域,左边控制虚拟摇杆从而控制player移动
if (Input.touches[i].position.x < Screen.width*0.3f)
{
//只要有左边区域被触摸了,不管Input.touchCount等于多少,都会执行下面
if (Input.touches [i].phase == TouchPhase.Began) {
moveCenter = Input.touches [i].position;//移动点位置为触摸点位置
moveBackPos.gameObject.SetActive (true);//显示摇杆背景
moveBackPos.position = moveCenter;//摇杆背景位置=移动点位置
joy.position = moveCenter;//摇杆位置=移动点位置
}
//手指触摸移动
if (Input.touches [i].phase == TouchPhase.Moved) {
Vector2 oppsitionVec = (Vector2)Input.touches [i].position - moveCenter; //移动向量
float length = Vector3.Magnitude(oppsitionVec); //向量长度
float radius = Mathf.Clamp(length,0,maxRadius);//限制半径=length且0<=length<=maxRadius
joy.position = moveCenter + oppsitionVec.normalized * radius;
}
//手指触摸离开
if (Input.touches [i].phase == TouchPhase.Ended) {
joy.position = moveCenter;
joy.localPosition = Vector3.zero;
moveBackPos.gameObject.SetActive (false);
}
}
}
}
}
}
然后在人物移动控制脚本里面修改一下就行
public EasyTouchMove etmove;
void FixedUpdate () {
//float x = Input.GetAxis(“Horizontal”);
//float z = Input.GetAxis (“Vertical”);
float x = etmove.Horizontal;
float z = etmove.Vertical;
(2) 视野控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AndoridTouchCamera : MonoBehaviour {
private int isforward; //视角放大/缩小
//两个手指的旧位置和新位置
private Vector2 oldPos1 = new Vector2();
private Vector2 oldPos2 = new Vector2();
private Vector2 newPos1 = new Vector2();
private Vector2 newPos2 = new Vector2();
//用于判断是否放大
bool isEnlarge (Vector2 oldPos1,Vector2 oldPos2,Vector2 newPos1,Vector2 newPos2)
{
float Pos_X1 = oldPos1.x - oldPos2.x;
float Pos_Y1 = oldPos1.y - oldPos2.y;
float length1 = Mathf.Sqrt (Pos_X1 * Pos_X1 + Pos_Y1 * Pos_Y1);
float Pos_X2 = newPos1.x - newPos2.x;
float Pos_Y2 = newPos1.y - newPos2.y;
float length2 = Mathf.Sqrt (Pos_X2 * Pos_X2 + Pos_Y2 * Pos_Y2);
if (length1 < length2)
return true;//放大手势
else
return false;//缩小手势
}
void Start(){
Input.multiTouchEnabled = true;//开启多点触控
}
void Update(){
if (Input.touchCount <= 0)
return;
if (Input.touchCount == 1)
return;
if (Input.touchCount > 1) {
//记录两个手指每帧的移动距离
Vector2 deltaDis1 = new Vector2 ();
Vector2 deltaDis2 = new Vector2 ();
for (int i = 0; i < 2; i++) {
Touch touch = Input.touches [i];
//限制触控范围,
if (touch.position.x>(0.33f*Screen.width)&&touch.position.x<(0.67*Screen.width))
{
if (touch.phase == TouchPhase.Ended) //手指离开触控
break;
if (touch.phase == TouchPhase.Moved) { //手指处于触控移动状态
if (i == 0) { //记录第一个手指新位置
newPos1 = touch.position;
deltaDis1 = touch.deltaPosition;
} else { //记录第二个手指新位置
newPos2 = touch.position;
deltaDis2 = touch.deltaPosition;
if (isEnlarge (oldPos1, oldPos2, newPos1, newPos2))
//放大手势,视野拉近
isforward = -1;
else
isforward = 1;
}
//备份上次触摸位置,用于对比
oldPos1 = newPos1;
oldPos2 = newPos2;
}
//缩放视角
//计算两个触摸点 新点和旧点的距离
float dis = Mathf.Abs(deltaDis1.x+deltaDis2.x)+Mathf.Abs(deltaDis1. y+deltaDis2.y);
//限制视角缩放的程度
if (Camera.main.fieldOfView >= 30 && Camera.main.fieldOfView <= 90)
{
//根据手势控制视野缩放
Camera.main.fieldOfView += isforward * (dis / 10);
if (Camera.main.fieldOfView < 30)
Camera.main.fieldOfView = 30;
if (Camera.main.fieldOfView > 90)
Camera.main.fieldOfView = 90;
}
}
}
}
}
}
来源:CSDN
作者:<somewhere>
链接:https://blog.csdn.net/qq_39745907/article/details/104010204