Input.GetTouch 获取触摸
static function GetTouch (index : int) : Touch
Description描述
Returns object representing status of a specific touch (Does not allocate temporary variables).
返回一个存放触摸信息的对象(不允许分配临时变量)。
1.手指移动控制物体移动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveCube : MonoBehaviour {
static int count;//定义touchCount数
public GameObject particle_;//定义存放cube对象
public Vector3 touchposition;//存储移动三维坐标值
// Update is called once per frame
void 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.01f, touchposition.y*0.01f, 0);//[color=Red]移动这个距离[/color]
}
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 30), "cishu:" + count.ToString());
GUI.Label(new Rect(10, 50, 100, 30), touchposition.ToString());
}
}
2.当用户在屏幕点击时实例一个炮弹
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;
}
}
}
3.当用户在屏幕点击时发射一条光线
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;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)