博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Unity3d for android]屏幕触摸事件
阅读量:6875 次
发布时间:2019-06-26

本文共 1652 字,大约阅读时间需要 5 分钟。

移动物体:

[csharp] 
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class example : MonoBehaviour {  
  5.     public float speed = 0.1F;  
  6.     void Update() {  
  7.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {  
  8.             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;  
  9.             transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);  
  10.         }  
  11.     }  
  12. }  


点击碰撞克隆

[csharp] 
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class example : MonoBehaviour {  
  5.     public GameObject projectile;  
  6.     void Update() {  
  7.         int i = 0;  
  8.         while (i < Input.touchCount) {  
  9.             if (Input.GetTouch(i).phase == TouchPhase.Began)  
  10.                 clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;  
  11.               
  12.             ++i;  
  13.         }  
  14.     }  
  15. }  


 

===================

[csharp] 
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class example : MonoBehaviour {  
  5.     public GameObject particle;  
  6.     void Update() {  
  7.         int i = 0;  
  8.         while (i < Input.touchCount) {  
  9.             if (Input.GetTouch(i).phase == TouchPhase.Began) {  
  10.                 Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);  
  11.                 if (Physics.Raycast(ray))  
  12.                     Instantiate(particle, transform.position, transform.rotation) as GameObject;  
  13.                   
  14.             }  
  15.             ++i;  
  16.         }  
  17.     }  
  18. }  


 

TouchPhase Enumeration  

Describes phase of a finger touch.

Values

A finger touched the screen.

A finger moved on the screen.

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

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

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.

 

本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366234,如需转载请自行联系原作者

你可能感兴趣的文章
ResulsetHandler九个实现类
查看>>
python 函数
查看>>
找出在使用临时表空间的SQL
查看>>
MapReduce 学习(一)
查看>>
专访李智慧:架构是最高层次的规划和难以改变的决定
查看>>
HTML5 入门基础
查看>>
Laravel 中的 Many-To-Many
查看>>
Codeforces 371C Hamburgers(二分基础题)
查看>>
django 自定义tag和filter
查看>>
FileWriter写数据路径问题及关闭和刷新方法的区别
查看>>
Page Layout里的javascript (jquery)不执行
查看>>
JS中的发布订阅模式
查看>>
解析JMeter的JTL文件
查看>>
1-N中1出现的次数
查看>>
springmvc自定义视图
查看>>
windows driver 映射大文件
查看>>
《R语言实战》读书笔记--第三章 图形初阶(一)
查看>>
MFC串口的编程 mscomm控件与SerialPort类
查看>>
乔恩与加菲猫引发的思考
查看>>
02-01官网静默模式安装WebLogic
查看>>