Unity3D圣典3.3版本-中文版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Input.GetTouch 获取触摸
# [Input](Input.html).GetTouch 获取触摸 static function *GetTouch* (index : int) : [Touch](../Touch/Touch.html) *Description* 描述 Returns object representing status of a specific touch (Does not allocate temporary variables). 返回一个存放触摸信息的对象(不允许分配临时变量)。 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public float speed = 0.1F; void Update() { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) { Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition; transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0); } } } ``` ``` // Moves object according to finger movement on the screen //根据手指在屏幕上移动来移动物体 var speed : float = 0.1; function Update () { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) { // Get movement of the finger since last frame //获取手指自最后一帧的移动 var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition; // Move object across XY plane //移动物体在XY平面 transform.Translate (-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0); } } ``` 另一个例子 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public GameObject projectile; void Update() { int i = 0; while (i < Input.touchCount) { if (Input.GetTouch(i).phase == TouchPhase.Began) clone = Instantiate(projectile, transform.position, transform.rotation); ++i; } } } ``` ``` // Instantiates a projectile whenever the user taps on the screen //实例一个炮弹,当用户在屏幕上点击时 var projectile : GameObject; function Update () { for (var i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase == TouchPhase.Began) { clone = Instantiate (projectile, transform.position, transform.rotation); } } } ``` * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public GameObject particle; void Update() { int i = 0; while (i < Input.touchCount) { if (Input.GetTouch(i).phase == TouchPhase.Began) { Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position); if (Physics.Raycast(ray)) Instantiate(particle, transform.position, transform.rotation); } ++i; } } } ``` ``` // Shoot a ray whenever the user taps on the screen //发射一条光线,当用户在屏幕上点击时 var particle : GameObject; function Update () { for (var i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase == TouchPhase.Began) { // Construct a ray from the current touch coordinates //构造一条光线,从当前的触摸坐标 var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position); if (Physics.Raycast (ray)) { // Create a particle if hit //如果击中,创建一个粒子 Instantiate (particle, transform.position, transform.rotation); } } } } ```
da
2022年5月23日 14:02
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码