大朋M2头盔触摸板开发

本文介绍如何在Unity中使用触摸屏输入控制游戏对象。详细解释了触摸板坐标范围划分及对应的游戏内方向控制,并提供了多点触控的实现方法。文章还探讨了不同触摸阶段的处理方式以及如何使游戏对象随触摸移动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

经过使用label进行测试后发现,触摸板的大致坐标范围为        右下(340,190)->    左上(2200,1200)

使用触摸板控制 玩家行走  时可以  根据 坐标将触摸板分为八个区域

左上,左下,右上,右下,前,后,左,右 八个方向



使用Input.Touch    来响应触摸事件 

Touch    的几种状态

TouchPhase Enumeration  

Describes phase of a finger touch.

Values

Began

A finger touched the screen.

Moved

A finger moved on the screen.

Stationary

A finger is touching the screen but hasn't moved.

Ended

A finger was lifted from the screen. This is the final phase of a touch.

Canceled

The system cancelled tracking for the touch, as when (for example) the user puts the device to her face or more than five touches happened simultaneously. This is the final phase of a touch.

多点触控时,下标是从0开始的,两个触控点下标就是0,1。

代码如下:

1
2
3
4
5
6
7
8
9
nt touchCount = 2;  // 触摸帧的数量
 
if (touchCount == Input.touchCount())<br>{
 
vector2 touchPosition1 = Input.GetTouch(0).position;
 
vector2 touchPosition2 = Input.GetTouch(1).position;
 
}

  

1.Input.touchCount 触摸随之增长,一秒50次增量。

2.Input.GetTouch(0).phase==TouchPhase.Moved 手指滑动中最后一帧滑动的状态是运动的。

3.TouchPhase  触摸的几个状态。

4.Touch.deltaPosition 增量位置(Input.GetTouch(0).deltaPosition)最后一帧滑动的值,只返回xy轴坐标,也可用vector3(z轴为0),所以一般用vector2接收。

复制代码
static var aa:int;
function Update () {
if(Input.touchCount>0)
{
print(Input.touchCount);
}
}
function OnGUI()
{
GUI.Label(Rect(34,34,34,34),"sdff");
}
复制代码

touchCount指的是触摸帧的数量。要注意的是:touch事件 只能在模拟器或者真机上运行(已测试通过),大约一秒钟touch不放。touchCount+50次左右。2.Input.touches 触摸列表。

复制代码
// Prints number of fingers touching the screen
//输出触摸在屏幕上的手指数量
function Update () {
var fingerCount = 0;
for (var touch : Touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
}
if (fingerCount > 0)
print ("User has " + fingerCount + " finger(s) touching the screen");
}
复制代码

3.让cube随着touch 移动代码:

复制代码
static var count:int; //定义touchCount数
var particle_:GameObject;//定义存放cube对象
var touchposition:Vector3; //存储移动三维坐标值
function Update () {
if(Input.touchCount>0)
{
count+=Input.touchCount;}
if((Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Moved)) //[color=Red]如果点击手指touch了  并且手指touch的状态为移动的[/color]
{
touchposition=Input.GetTouch(0).deltaPosition;  //[color=Red]获取手指touch最后一帧移动的xy轴距离[/color]
particle_.transform.Translate(touchposition.x*0.01,touchposition.y*0.01,0);//[color=Red]移动这个距离[/color]
}}
function OnGUI()
{
GUI.Label(Rect(10,10,100,30),"cishu:"+count.ToString());
GUI.Label(Rect(10,50,100,30),touchposition.ToString());
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值